[英]TCP Socket doesn't receive Data in c
I got the task to create some Chat Client. 我得到了创建一些聊天客户端的任务。 The server is given and code unknown (only got the executable).
给出服务器并且代码未知(仅获得可执行文件)。 The Client/Server communicates via TCP.
客户端/服务器通过TCP进行通信。 First the Client connects to the server and sends his username.
首先,客户端连接到服务器并发送其用户名。 The Server will answer with some Statuscode.
服务器将回答一些状态码。 At this state I'm stuck.
在这种状态下,我被困住了。 I created the socket and connect to the server.
我创建了套接字并连接到服务器。 The server shows, that it receives the username but my client doesn't get any answer.
服务器显示,它收到用户名但我的客户端没有得到任何答案。
#define MAX_USERNAME 11
#define MAX_MSG_SIZE 1001
#define SERVERIP "localhost"
#define SERVERPORT 5000
[...]someFunctions()[...]
int _tmain(int argc, char* argv[])
{
char defaulthost[] = "localhost";
hostent *host;
protoent *protocol;
SOCKADDR_IN server_addr;
SOCKET socket_d;
unsigned short port;
char *hostname;
int n;
int c;
int connectCode;
char username[MAX_USERNAME];
char message[MAX_MSG_SIZE];
long conn;
long msgSize;
[...]
//read in username
[...]
// Connect to Server
RNP_Init(); // intializes winsock
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
port = SERVERPORT;
if (port > 0){
server_addr.sin_port = htons(port);
}
else {
fprintf(stdout,"Ungültige Portnummer\n");
RNP_Cleanup();
exit(EXIT_FAILURE);
}
hostname = defaulthost;
conn = getAddress(hostname,&server_addr);
if(conn == SOCKET_ERROR){
fprintf(stdout,"IP fuer %s nicht aufgeloest",hostname);
RNP_Cleanup();
exit(EXIT_FAILURE);
}
else{
fprintf(stdout,"IP aufgeloest");
}
socket_d = socket(AF_INET, SOCK_STREAM, 0);
if (socket_d == INVALID_SOCKET) {
RNP_Error(RNP_E_SOCK, "Fehler bei Generierung des Sockets\n");
RNP_Cleanup();
exit(EXIT_FAILURE);
}
else {
fprintf(stdout, "Socket erfolgreich erstellt!\n");
}
conn = connect(socket_d, (SOCKADDR*)&server_addr,sizeof(SOCKADDR));
if(conn==SOCKET_ERROR){
printf("Fehler: connect gescheitert, ERROR: %d\n", WSAGetLastError());
RNP_Cleanup();
fprintf(stdout,"Taste druecken zum beenden\n");
getch();
exit(EXIT_FAILURE);
}
else{
printf("Verbunden mit Server!!\n");
}
while(conn != SOCKET_ERROR) {
fprintf(stdout,"Sende Nutzernamen...\n");
send(socket_d, username, strlen(username), 0);
fprintf(stdout,"Username gesendet: %s mit der Laenge %d\n", username, strlen(username));
msgSize = recv(socket_d,message,1001,0);
if(msgSize==0){
fprintf(stdout,"Verbindung vom Server getrennt\n");
break;
}
if(msgSize==SOCKET_ERROR){
fprintf(stdout,"Error: %d\n",WSAGetLastError());
break;
}
message[conn]='\0';
printf("Server antwortet: %s\n",message);
}
[...]
The problem is here: 问题出在这里:
message[conn]='\\0';
If conn
equals 0, you will not be able to "see" the data. 如果
conn
等于0,您将无法“看到”数据。 You should not use conn
but msgSize
, which contains the number of received bytes. 您不应该使用
conn
而是使用msgSize
,其中包含接收的字节数。
message[msgSize]='\\0';
EDIT: You should take care of Barmar
's advices. 编辑:你应该照顾
Barmar
的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.