Feb 5, 2013 at 6:35am UTC
Hi everyone, I got errors when I compiled my server and client program.
This is my server program. I am also using ubuntu 11.10.
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <arpa/inet.h>
#define PORT 4547
/*int parseARGS(char **args, char *line)
{
int tmp = 0;
args[tmp] = strtok(line, ":" );
while ((args[++tmp] = strtok(NULL, ":" )) != NULL);
return tmp -1;
}*/
int main(int argc, char *argv[])
{
//char *header[4096];
int listenSOCKET;
int connectSOCKET;
socklen_t clientADDRESSLENGTH;
struct sockaddr_in clientADDRESS, serverADDRESS;
char recvBUFF[4096];
//Image *recv_img;
//char *filename, *filesize;
//FILE *recvFILE;
//int received = 0;
//char tempstr[4096];
//int count1=1, count2=1, percent;
listenSOCKET = socket(AF_INET, SOCK_STREAM, 0);
if (listenSOCKET <0)
{
printf("Cannot create socket\n");
close(listenSOCKET);
return 1;
}
//PORT = atoi(argv[1]);
serverADDRESS.sin_family = AF_INET;
serverADDRESS.sin_addr.s_addr = inet_addr("172.17.111.3");
serverADDRESS.sin_port = htons(PORT);
if (bind(listenSOCKET, (struct sockaddr *)&serverADDRESS, sizeof(serverADDRESS))<0)
{
printf("Cannot bind socket\n");
close(listenSOCKET);
return 1;
}
listen(listenSOCKET, 5);
clientADDRESSLENGTH = sizeof(clientADDRESS);
connectSOCKET = accept(listenSOCKET,(struct sockaddr *)&clientADDRESS, &clientADDRESSLENGTH);
if (connectSOCKET<0)
{
printf("Cannot accpet connection\n");
close(listenSOCKET);
return 1;
}
bzero(recvBUFF,4096);
while(1)
{
if(recv(connectSOCKET,recvBUFF,1,0) == 0)
printf("ERROR on receiving");
//memcpy(recv_img,recvBUFF, sizeof(recvBUFF));
close(listenSOCKET);
return 0;
}
}
I got an error like cannot bind socket.
I run the program like ./server4.o 4547.
why is that so? If anyone know how to solve it. pls kindly tell me. thanks
Feb 5, 2013 at 7:24am UTC
Are you running client and server on the same PC...?
Feb 5, 2013 at 9:12am UTC
Instead of this,
serverADDRESS.sin_addr.s_addr = inet_addr("172.17.111.3" );
please use local address this will solve your problem..
serverADDRESS.sin_addr.s_addr = inet_addr("127.0.0.1" );
Last edited on Feb 5, 2013 at 9:12am UTC