Hi All,
I have written a client server program which works in a strange way, it will be helpful for me to understand if somebody give information about the error in the code.
When the connection is established frm many clients to the server, the server behaves strangely producing the ip address of the FIRST CLIENT alone as 127.0.0.0 and also before i send any message, a blank message is sent from the FIRST client to server and i receive acknowledgement from server for that blank messages and after this what ever message i send from the first client or any client is received at the server correctly and also i receive the ip address of all other clients correctly from the server Except the first client as i mentioned previously.
I can do my coding in some other way to avoid this but i need to know y this particular code which is intented to work correctly is behaving strangely.
Question1: Y is the server showing the ip address of first client as 127.0.0.0?
Question2: Y is the blank message sent to the server from first client?
Code:
Server Program:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stddef.h>
#include<arpa/inet.h>
/* things to be noted 1. File descriptor 2. Network byte order 3.What are the different family exits */
/* any problem at any part of the code this will display the error message */
void error(char *message)
{
perror(message);
exit(1);
}
void main()
{
int sockfd,newsockfd,port_no,n,client_len;
char buffer[255];
struct sockaddr_in serv_addr,client_addr;
pid_t PID;
sockfd = socket(AF_INET, SOCK_STREAM, 0); /*socket(domain,type,protocol) and if it is possible to get protocol details from 1st and 2nd use 0*/
/* socket function returns an file descriptor no --> which means its a new file created which is accessible only by the kernel memory*/
if(sockfd<0)
{
error("Error in opening of socket");
}
printf("Enter the portno");
scanf("%d",&port_no);
/*definition of four fields which are present in sockaddr_in structure*/
serv_addr.sin_family = AF_INET; /*this information we specify that it belongs to tcp or udp*/
serv_addr.sin_port = htons(port_no); /*htons is used because we may need to change it to network byte order which is always big endian*/
serv_addr.sin_addr.s_addr = INADDR_ANY; /*this means that server will serve to the specific port which is mentioned regardless of what its ip address is*/
/*binding the socket address with the socket field descriptor which we have already created*/
if(bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) <0)
{
error("Error in binding");
}
/* added newly */
while(1)
{
listen(sockfd,5);
newsockfd = accept(sockfd, (struct sockaddr *) &client_addr,&client_len); /*this will actually receive the client address and the length of it*/
printf("The client ip address is:");
printf("%s\n", inet_ntoa(client_addr.sin_addr));
PID=fork();
if(!PID)
{
printf("Forking the new child");
for(;;)
{
bzero(buffer,256);
fflush(buffer);
n = read(newsockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message\n",18);
if (n < 0) error("ERROR writing to socket");
bzero(buffer,256);
}
}
}
}
Client Program:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<stddef.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
void main()
{
int sockfd, port_no, n,option;
char ip_adr[15]="10.121.9.240";
struct sockaddr_in serv_addr;
char buffer[255];
printf("Enter the portno");
scanf("%d",&port_no);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0)
{
error("Error in opening of socket");
}
/* gets(ip_adr);*/
serv_addr.sin_addr.s_addr =inet_addr(ip_adr); /* the obtained ip address which is a dotted string is converted to unsigned long.*/
serv_addr.sin_family = AF_INET; /* this information we specify that it belongs to tcp or udp*/
serv_addr.sin_port = htons(port_no); /*htons is used because we may need to change it to network byte order which is always big endian*/
/* We need not have a binding in this because we need not have to include a specified port*/
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) /* same as accept here in the client we establish a connection */
error("ERROR connecting");
else
printf("Connected");
for(;;)
{
printf("\nPlease enter the message which u need to send:\n");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,strlen(buffer));
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s",buffer);
}
}
If anybody could reply for my questions soon i could proceed further with this....
Thanks in Advance.
regards,
Sudharsanam.