-
Understanding Sockets
I have taken some sample code from the web and tried to incorporate it into an application in order to learn how to use sockets for comms between two PCs.
My Server code is:
Code:
int CainLANServerComms(int a, int b)
{
WSAStartup(0x0101,&wsdata);
char aString[10], bString[10];
itoa(a,aString,10);
itoa(b,bString,10);
char buffer [50];
for(int i = 0;i < strlen(aString); i++)
{
buffer [i] = aString [i];
}
buffer [i] = 'a';
for(i = 0;i < strlen(bString); i++)
{
buffer [i + strlen(aString) + 1] = bString [i];
}
buffer [i + strlen(aString) + 1 ] = '\0';
if (a == -1)
{
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) /* calls socket() */
{
MessageBox(0,"Socket Error","Error",0);
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT); /* Remember htons() from "Conversions" section? =) */
server.sin_addr.s_addr = INADDR_ANY; /* INADDR_ANY puts your IP address automatically */
if(bind(fd,(struct sockaddr*)&server,sizeof(struct sockaddr)) == -1) /* calls bind() */
{
MessageBox(0,"Bind Error","Error",0);
return -1;
}
if(listen(fd,BACKLOG) == -1) /* calls listen() */
{
MessageBox(0,"Listen Error","Error",0);
return -1;
}
sin_size = sizeof(struct sockaddr_in);
if ((fd2 = accept(fd,(struct sockaddr *)&client,&sin_size)) == -1) /* calls accept() */
{
MessageBox(0,"Accept Error","Error",0);
return -1;
}
while (1)
{
if (send(fd2,"buffer",22,0))
{
MessageBox(0,"Sent","Error",0);
break;
}
}
}
else
{
while (1)
{
if (send(fd2,buffer,22,0))
{
MessageBox(0,buffer,"Error",0);
break;
}
}
}
WSACleanup();
return -1;
}
and my Client code is:
Code:
char* CainLANComms(bool Flag)
{
if (!Flag)
{
WSAStartup(0x0101,&wsdata);
in_addr toBeResolved;
toBeResolved.s_addr = inet_addr("/*Edited out my IP address*/");
he = gethostbyaddr((const char *)&toBeResolved, sizeof(struct in_addr),AF_INET) ; /* calls gethostbyname() */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) /* calls socket() */
{
MessageBox(0,"Socket Error","Error",0);
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT); /* htons() is needed again */
server.sin_addr = *((struct in_addr *)he->h_addr); /* he->h_addr passes "*he"'s info to "h_addr" */
if(connect(sock,(struct sockaddr *)&server,sizeof(struct sockaddr)) == -1) /* calls connect() */
{
MessageBox(0,"Connection Error","Error",0);
}
}
if ((numbytes = recv(sock,buf,MAXDATASIZE,0)) == -1) /* calls recv() */
{
MessageBox(0,"Receiving Error","Error",0);
}
buf[numbytes]='\0';
MessageBox(0,buf,"Error",0);
WSACleanup();
return buf;
}
Basically I open up both the Server and Client apps on my screen and they each have a window. I click the Server window and int CainLANServerComms(int a, int b) is called with -1 as the paramters. This sets up the connection and sends "buffer". I click on the Client window and CainLANComms(bool Flag) is called with '0' as the parameter. This sets up the connection and "buffer" is received and displayed. Then I click on Server a second time. The parameters now are two positive integers. This means that the connection setup is bypassed and send() is called. A message box is displayed by the server app with the correct data in. Then I click the Client app and CainLANComms(bool Flag) with the parameter set to 1. This bypasses the setup and calls recv(). The client app then gives me a messagebox displaying "receiving error" beffore crashing.
Is there some fundamental operation of sockets that I am misunderstanding?
-
Post the line that crashes.
Kuphryn
-
It's very difficult to say. The problem is when the Client code runs for the second time. This code:
Code:
if ((numbytes = recv(sock,buf,MAXDATASIZE,0)) == -1) /* calls recv() */
{
MessageBox(0,"Receiving Error","Error",0);
}
buf[numbytes]='\0';
MessageBox(0,buf,"Error",0);
WSACleanup();
return buf;
disaplys the "receiving error" messagebox and I don't know why. The messagebox MessageBox(0,buf,"Error",0); then displays the data stored in buf from the first call shortly before the program crashes. There should be data to be received as the Server code
Code:
if (send(fd2,buffer,22,0))
{
MessageBox(0,buffer,"Error",0);
break;
}
displays a messagebox containing the correct data even before the client window is clicked for the second time.
-
Ahh no worries, I have sorted it. IT was cos I was calling WSACleanup() at the end of the first call. Thanks anyway Kuphryn.