Hello Friends I am a newbie to socket programming in c.I 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.
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. 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;
}
\0
after the string that marks where it ends. Sostrlen()
of"data"
is 4 because it has 4 characters, but its total size is actually 5 bytes. You have to send the\0
too, otherwise thebuf
in server-side won't be a valid C string and may cause undefined behaviour when you try toprintf()
it. – Havenardsocket()
should receiveIPPROTO_TCP
as third parameter, not0
. – Havenard