我正試圖使用套接字編程實現簡單的聊天,其中「輸入」鍵將響應從客戶端發送到服務器,反之亦然。下面的代碼按照預期使用特殊字符(如'*')作爲緩衝區,但是當我嘗試使用'\ n'時,不會發送消息。我必須錯誤地執行「輸入」部分。有關如何解決此問題的任何想法?Socket編程簡單聊天C++
服務器:
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main()
{
int client, server;
int portNum = 1500;
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
struct sockaddr_in server_addr;
socklen_t size;
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0)
{
cout << "\nError establishing socket..." << endl;
exit(1);
}
cout << "\n=> Socket server has been created..." << endl;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(portNum);
if ((bind(client, (struct sockaddr*)&server_addr,sizeof(server_addr))) < 0)
{
cout << "=> Error binding connection, the socket has already been established..." << endl;
return -1;
}
size = sizeof(server_addr);
cout << "=> Looking for clients..." << endl;
listen(client, 1);
int clientCount = 1;
server = accept(client,(struct sockaddr *)&server_addr,&size);
// first check if it is valid or not
if (server < 0)
cout << "=> Error on accepting..." << endl;
while (server > 0)
{
strcpy(buffer, "=> Server connected...\n");
send(server, buffer, bufsize, 0);
cout << "=> Connected with the client #" << clientCount << ", you are good to go..." << endl;
cout << "\n=> Enter # to end the connection\n" << endl;
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
do {
cout << "\nServer: ";
do {
cin >> buffer;
send(server, buffer, bufsize, 0);
if (*buffer == '#') {
send(server, buffer, bufsize, 0);
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
} while (!isExit);
cout << "\n\n=> Connection terminated with IP " << inet_ntoa(server_addr.sin_addr);
close(server);
cout << "\nGoodbye..." << endl;
isExit = false;
exit(1);
}
close(client);
return 0;
}
客戶:
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
using namespace std;
int main()
{
int client;
int portNum = 1500; // NOTE that the port number is same for both client and server
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
char* ip = "192.168.0.8";
struct sockaddr_in server_addr;
client = socket(AF_INET, SOCK_STREAM, 0);
if (client < 0)
{
cout << "\nError establishing socket..." << endl;
exit(1);
}
cout << "\n=> Socket client has been created..." << endl;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portNum);
if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0)
cout << "=> Connection to the server port number: " << portNum << endl;
cout << "=> Awaiting confirmation from the server..." << endl; //line 40
recv(client, buffer, bufsize, 0);
cout << "=> Connection confirmed, you are good to go...";
cout << "\n\n=> Enter # to end the connection\n" << endl;
do {
cout << "Client: ";
do {
cin >> buffer;
send(client, buffer, bufsize, 0);
if (*buffer == '#') {
send(client, buffer, bufsize, 0);
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
cout << "Server: ";
do {
recv(client, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
cout << endl;
} while (!isExit);
cout << "\n=> Connection terminated.\nGoodbye...\n";
close(client);
return 0;
}
編輯:我做了下面的回答中引用的變化,我仍然沒有得到預期的結果。
當我按下輸入信息不被髮送到服務器。但是,當我使用「buffer ='*'」運行相同的代碼時,它按預期工作。
至少檢查send和recv的返回值。此外,你正在發送你的整個緩衝區,即使當它大部分都是垃圾時,我認爲這些作品是有價值的,但有點古怪。 – hyde
我剛編譯並運行了最近的代碼(如此評論),併發送/接收數據。你的問題是一個應用程序邏輯錯誤,而不是技術誤用套接字API。你試圖達到什麼樣的行爲?當你從客戶端發送時,你是否期望它立即在服務器上回顯,反之亦然? –