[英]Server cannot receive the clients message in c winsock programming
Hello Friends I am a newbie to socket programming in cI read a few tutorials in the net and started working.The programs below are my socket client and server programs using winsock2 library in WINDOWS.你好朋友我是c中套接字编程的新手我在网上阅读了一些教程并开始工作。下面的程序是我在WINDOWS中使用winsock2库的套接字客户端和服务器程序。
Whenever i run the program, there is no error until the server accepts the clients connection.But after the client sends the message the server cannot receive the message.每当我运行程序时,在服务器接受客户端连接之前没有错误。但是在客户端发送消息后,服务器无法接收消息。 The WSAGetLastError() function returns the error code as 10038.I tried changing the port, increasing and decreasing the message size.
WSAGetLastError() 函数返回错误代码为 10038。我尝试更改端口,增加和减少消息大小。 help me friends.
帮帮我的朋友。
Server code服务器代码
#include<stdio.h>
#include<conio.h>
#include<WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsa;
int r;
char buf[4];
WSAStartup(MAKEWORD(2,2),&wsa);
SOCKET s= socket(AF_INET,SOCK_STREAM,0);
if ( s==INVALID_SOCKET)
{
printf("\n failed to create socket error code : %d ",WSAGetLastError());
WSACleanup();
return 1;
}
sockaddr_in sin,cl;
sin.sin_family=AF_INET;
sin.sin_port=htons(80);
sin.sin_addr.s_addr=INADDR_ANY;
if(bind (s,(sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR)
{
printf("\nSocket not Bound\n");
WSACleanup();
return 1;
}
r=listen(s,2);
if(r==SOCKET_ERROR)
{
printf("\nListening Failed\n");
WSACleanup();
return 1;
}
printf("\n Listening\n");
SOCKET client;
r= sizeof(sockaddr_in);
printf("\n Ready to accept");
while (client=accept(s,(sockaddr *)&cl,&r)!= INVALID_SOCKET)
{
printf("\nNew client found\n\n");
if(client == SOCKET_ERROR)
{
printf("\nError connecting client error code : %d ",WSAGetLastError());
WSACleanup();
return 1;
}
r=recv(client,buf,4,0);
if(r==SOCKET_ERROR)
{
printf("\nSocket not connected error code : %d",WSAGetLastError());
WSACleanup();
return 1;
}
printf("%s\n",buf);
}
getchar();
return 0;
}
Client code客户端代码
#include <stdio.h>
#include <conio.h>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsa;
WSAStartup(MAKEWORD(2,2),&wsa);
int r;
char *buf="data";
SOCKET t=socket(AF_INET,SOCK_STREAM,0);
if(t==SOCKET_ERROR)
{
printf("\nFailed socket error code : %d ",WSAGetLastError());
WSACleanup();
return 1;
}
sockaddr_in server;
server.sin_family=AF_INET;
server.sin_addr.s_addr=inet_addr("192.168.0.142");
server.sin_port=htons(80);
printf("\nConnecting\n");
r=connect(t,(sockaddr *)&server,sizeof(server));
if(r==SOCKET_ERROR)
{
printf("\nFailed to connect error code : %d ",WSAGetLastError());
WSACleanup();
return 1;
}
printf("\n connected\n");
printf("\nSending data\n");
r=send(t,buf,strlen(buf),0);
if (r==SOCKET_ERROR)
{
printf("\nFailed to connect error code : %d ",WSAGetLastError());
WSACleanup();
return 1;
}
printf("\n Data sent\n");
return 0;
}
First we look up what 10038 means.首先我们查一下 10038 是什么意思。
SAENOTSOCK
10038 (0x2736)
An operation was attempted on something that is not a socket.
OK, so you think you have a socket, but you really don't in one of your calls.好的,所以你认为你有一个套接字,但你真的没有在你的一个电话中。 You weren't very specific on which
WSAGetLastError()
function was returning 10038, but I'm going to guess the call after this one:您对
WSAGetLastError()
函数返回 10038 不是很具体,但我会猜测在此之后的调用:
while (client=accept(s,(sockaddr *)&cl,&r)!= INVALID_SOCKET)
In C, the !=
is handled first, and then the assignment.在 C 中,首先处理
!=
,然后是赋值。 So you end up with the socket returned by accept
being compared to INVALID_SOCKET
, and then that boolean value being assigned to client
.因此,您最终
INVALID_SOCKET
accept
返回的套接字与INVALID_SOCKET
进行比较,然后将该布尔值分配给client
。 You want the socket to be assigned and then compared, so you should use parentheses around the assignment:您希望分配套接字然后进行比较,因此您应该在分配周围使用括号:
while ((client = accept(s,(sockaddr *)&cl,&r)) != INVALID_SOCKET)
Also, socket()
returns INVALID_SOCKET
and not SOCKET_ERROR
, although confusing the two won't cause a problem by coincidence.此外,
socket()
返回INVALID_SOCKET
而不是SOCKET_ERROR
,尽管将两者混淆不会因巧合而导致问题。
Further, you need to null-terminate the string on the receiver side if you are going to print it.此外,如果您要打印它,您需要在接收方端以空字符结尾。
char buf[5]; /* 4 characters + 1 null-terminator */
...
buf[sizeof(buf) - 1] = '\0'; /* enforce that the buffer is always null terminated regardless of what the sender sends. */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.