Hi
I am using my simple clientserver program (uses sock_stream) to transmit
some data. Server will receive data and increment an index, each time it
receives a data.
The server is made concurrent. I can send data to server without any
problem, but as soon as, the server receives data 398 times, it wont receive
any more and I should restart my server program and listen in some different
port. Let say I start my server program and listening on a port, and from
client side, sending 500 times 1000 bytes data. The server will receive 398
times, but wont receive the rest of data. I dont know what is going wrong.
I have also other question, if I make the same server as a regular
(iterative) server it will receive all data, but in a strange way. Let say,
the server is listening in a port and the client sending 500 * 1000 bytes,
the server will receive the first data packet and show the index, but will
wait until it has received the entire data packet (499) and later show
index. It is not receiving one by one. I dont know if it should be like
that or there is some thing wrong.
I have my code for both concurrent and iterative server below.
Thanks for your concern David
/* Here is the Concurrent server */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#define mesage
int sockfd, newsockfd, portno, clilen;
char buffer[5000];
struct sockaddr_in serv_addr, cli_addr;
int n, i ; int nr;
int childpid;
void error(char *msg)
{
perror(msg);
exit(1);
void send_data(){Quote:}
i = i+1;
if (newsockfd < 0){
error("ERROR on accept");
exit(0);
}
if((n=recv(newsockfd,buffer,5000,0))==-1)
{
perror("recv");
exit(1);
}
// printf("Here is the %d message: %s\n",i,buffer);
printf("Here is the %d message with length %d\n",i,strlen(buffer));
//rearead the buffer
do {
bzero(buffer, sizeof buffer);
if((n=recv(newsockfd,buffer,5000,0))==-1)
{
perror("recv");
exit(1);
}
// printf("Here is the %d message: %s\n",i,buffer);
if (strlen(buffer) > 0)
printf("Here is the %d message with length %d\n",i,strlen(buffer));
}while ((strlen(buffer))!= 0);
int main(int argc, char *argv[])Quote:}
{
i = 0;
nr =0;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,1000);
bzero(buffer,5000);
while(1){
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if ((childpid = fork()==0))
{
close(sockfd);
send_data();
close(newsockfd);
exit(0);
}
close(newsockfd);
i = i+1;
}
/////////////////////Quote:}
/* Her is the iterative server */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#define mesage
int sockfd, newsockfd, portno, clilen;
char buffer[5000];
struct sockaddr_in serv_addr, cli_addr;
int n, i ; int nr;
void error(char *msg)
{
perror(msg);
exit(1);
void send_data(){Quote:}
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if (newsockfd < 0){
error("ERROR on accept");
exit(0);
}
i++;
if((n=recv(newsockfd,buffer,5000,0))==-1)
{
perror("recv");
exit(1);
}
// printf("Here is the %d message: %s\n",i,buffer);
printf("Here is the %d message with length %d\n",i,strlen(buffer));
//rearead the buffer
do {
bzero(buffer, sizeof buffer);
if((n=recv(newsockfd,buffer,5000,0))==-1)
{
perror("recv");
exit(1);
}
// printf("Here is the %d message: %s\n",i,buffer);
printf("Here is the %d message with length %d\n",i,strlen(buffer));
}while ((strlen(buffer))!= 0);
int main(int argc, char *argv[])
{
i = 0;
nr =0;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,10);
bzero(buffer,5000);
while(1){
clilen = sizeof(cli_addr);
send_data();
}
////////////////////////////////////////////Quote:}