A
Andre
Hi All,
I have written these very simple server and client, but the server
always crashes at the 2nd loop when it tries to bind again, and issues
an "Address already in use" error message.
But if I change
"if(recv(remoteSocketFd,(void *)
&messageToReceive,messageToReceiveLength,0) < 0)"
to
"if(recv(remoteSocketFd,(void *)
&messageToReceive,messageToReceiveLength - 1,0) < 0)"
the server is able to bind again in every single loop, but off course
it doesn't receive the correct value sent by the client.
How can this issue be corrected?
Thanks in advance,
Andre
// server
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#define CONNECTION_QUEUE_SIZE 5
#define SUCCESSED 1
#define ERROR 0
#define PORT 4000
#define HOST "127.0.0.1"
int main(int argc, char * argv[])
{
unsigned int messageToReceive;
int messageToReceiveLength;
int localSocketFd;
int remoteSocketFd;
struct sockaddr_in targetAddress;
struct sockaddr_in localServerAddress;
struct sockaddr_in remoteClientAddress;
int remoteClientAddressLength;
while(1)
{
remoteClientAddressLength = sizeof(struct sockaddr_in);
localSocketFd = socket(AF_INET, SOCK_STREAM, 0);
if(localSocketFd < 0)
{
return ERROR;
}
memset((void *)&localServerAddress, 0, sizeof(struct
sockaddr_in));
localServerAddress.sin_family = AF_INET;
localServerAddress.sin_port = htons(PORT);
localServerAddress.sin_addr.s_addr = INADDR_ANY;
if(bind(localSocketFd,
(struct sockaddr *)&localServerAddress,
sizeof(localServerAddress)) < 0)
{
printf("%s\n", strerror(errno));
return ERROR;
}
if(listen(localSocketFd, CONNECTION_QUEUE_SIZE))
{
return ERROR;
}
remoteSocketFd = accept(localSocketFd,
(struct sockaddr *)&remoteClientAddress,
(socklen_t*)&remoteClientAddressLength);
if(remoteSocketFd < 0)
{
return ERROR;
}
messageToReceiveLength = sizeof(messageToReceive);
if(recv(remoteSocketFd,
(void *)&messageToReceive,
messageToReceiveLength,
0) < 0)
{
return ERROR;
}
if(localSocketFd != -1)
{
if(close(localSocketFd) != 0)
{
return ERROR;
}
}
if(remoteSocketFd != -1)
{
if(close(remoteSocketFd) != 0)
{
return ERROR;
}
}
}
return SUCCESSED;
}
// client
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define CONNECTION_QUEUE_SIZE 5
#define SUCCESSED 1
#define ERROR 0
#define PORT 4000
#define HOST "127.0.0.1"
#define NUMBER 10
int main(int argc, char * argv[])
{
struct hostent * hostAddress;
struct sockaddr_in targetAddress;
int localSocketFd;
int remoteSocketFd;
unsigned int messageToSend = htonl(NUMBER);
while(1)
{
remoteSocketFd = socket(AF_INET, SOCK_STREAM, 0);
if(remoteSocketFd < 0)
{
return ERROR;
}
hostAddress = gethostbyname(HOST);
if(!hostAddress)
{
return ERROR;
}
memset((void *)&targetAddress, 0, sizeof(struct sockaddr_in));
targetAddress.sin_family = AF_INET;
targetAddress.sin_port = htons(PORT);
memcpy((void *)&targetAddress.sin_addr.s_addr,
(void *)hostAddress->h_addr,
hostAddress->h_length);
if(connect(remoteSocketFd,
(struct sockaddr *)&targetAddress,
sizeof(struct sockaddr)) == -1)
{
printf("1\n");
return ERROR;
}
if(send(remoteSocketFd,
(void *)&messageToSend,
sizeof(messageToSend),
0) < 0)
{
printf("2\n");
return ERROR;
}
if(remoteSocketFd != -1)
{
if(close(remoteSocketFd) != 0)
{
printf("3\n");
return ERROR;
}
}
}
return SUCCESSED;
}
I have written these very simple server and client, but the server
always crashes at the 2nd loop when it tries to bind again, and issues
an "Address already in use" error message.
But if I change
"if(recv(remoteSocketFd,(void *)
&messageToReceive,messageToReceiveLength,0) < 0)"
to
"if(recv(remoteSocketFd,(void *)
&messageToReceive,messageToReceiveLength - 1,0) < 0)"
the server is able to bind again in every single loop, but off course
it doesn't receive the correct value sent by the client.
How can this issue be corrected?
Thanks in advance,
Andre
// server
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#define CONNECTION_QUEUE_SIZE 5
#define SUCCESSED 1
#define ERROR 0
#define PORT 4000
#define HOST "127.0.0.1"
int main(int argc, char * argv[])
{
unsigned int messageToReceive;
int messageToReceiveLength;
int localSocketFd;
int remoteSocketFd;
struct sockaddr_in targetAddress;
struct sockaddr_in localServerAddress;
struct sockaddr_in remoteClientAddress;
int remoteClientAddressLength;
while(1)
{
remoteClientAddressLength = sizeof(struct sockaddr_in);
localSocketFd = socket(AF_INET, SOCK_STREAM, 0);
if(localSocketFd < 0)
{
return ERROR;
}
memset((void *)&localServerAddress, 0, sizeof(struct
sockaddr_in));
localServerAddress.sin_family = AF_INET;
localServerAddress.sin_port = htons(PORT);
localServerAddress.sin_addr.s_addr = INADDR_ANY;
if(bind(localSocketFd,
(struct sockaddr *)&localServerAddress,
sizeof(localServerAddress)) < 0)
{
printf("%s\n", strerror(errno));
return ERROR;
}
if(listen(localSocketFd, CONNECTION_QUEUE_SIZE))
{
return ERROR;
}
remoteSocketFd = accept(localSocketFd,
(struct sockaddr *)&remoteClientAddress,
(socklen_t*)&remoteClientAddressLength);
if(remoteSocketFd < 0)
{
return ERROR;
}
messageToReceiveLength = sizeof(messageToReceive);
if(recv(remoteSocketFd,
(void *)&messageToReceive,
messageToReceiveLength,
0) < 0)
{
return ERROR;
}
if(localSocketFd != -1)
{
if(close(localSocketFd) != 0)
{
return ERROR;
}
}
if(remoteSocketFd != -1)
{
if(close(remoteSocketFd) != 0)
{
return ERROR;
}
}
}
return SUCCESSED;
}
// client
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define CONNECTION_QUEUE_SIZE 5
#define SUCCESSED 1
#define ERROR 0
#define PORT 4000
#define HOST "127.0.0.1"
#define NUMBER 10
int main(int argc, char * argv[])
{
struct hostent * hostAddress;
struct sockaddr_in targetAddress;
int localSocketFd;
int remoteSocketFd;
unsigned int messageToSend = htonl(NUMBER);
while(1)
{
remoteSocketFd = socket(AF_INET, SOCK_STREAM, 0);
if(remoteSocketFd < 0)
{
return ERROR;
}
hostAddress = gethostbyname(HOST);
if(!hostAddress)
{
return ERROR;
}
memset((void *)&targetAddress, 0, sizeof(struct sockaddr_in));
targetAddress.sin_family = AF_INET;
targetAddress.sin_port = htons(PORT);
memcpy((void *)&targetAddress.sin_addr.s_addr,
(void *)hostAddress->h_addr,
hostAddress->h_length);
if(connect(remoteSocketFd,
(struct sockaddr *)&targetAddress,
sizeof(struct sockaddr)) == -1)
{
printf("1\n");
return ERROR;
}
if(send(remoteSocketFd,
(void *)&messageToSend,
sizeof(messageToSend),
0) < 0)
{
printf("2\n");
return ERROR;
}
if(remoteSocketFd != -1)
{
if(close(remoteSocketFd) != 0)
{
printf("3\n");
return ERROR;
}
}
}
return SUCCESSED;
}