Ok I've attached all the three files. On Mon, Oct 12, 2009 at 7:26 AM, Gao Free_Wind <gfree.wind@xxxxxxxxx> wrote: > Because there is no head file "unp.h", so the compiling failed. > > On Sat, Oct 10, 2009 at 6:24 PM, chandan apsangi <chandan.apsangi@xxxxxxxxx> > wrote: >> >> I tried with a single client-server also , but the same problem exists. >> write() to the socket on client's side is returning the number of >> bytes written correctly, but on server side read( ) remains in blocked >> state. >> >> Again ACK is sent from the server side for the data , but the data is >> not reaching the server process. >> >> Attached are the source files - client.c , server.c and lib.c has >> some common function definitions. >> >> On Sat, Oct 10, 2009 at 2:26 PM, Gao Free_Wind <gfree.wind@xxxxxxxxx> >> wrote: >> > Please show your source codes. >> > And i could give your some advices here. >> > 1. Debug with one client, only use your 1st client; >> > 2. Check the return value when 1st client send data to server. >> > >> > On Sat, Oct 10, 2009 at 4:34 PM, chandan apsangi >> > <chandan.apsangi@xxxxxxxxx> >> > wrote: >> >> >> >> Hi, >> >> >> >> I was trying out a simple client - server program using Linux sockets. >> >> Server basically echoes back whatever it receives from the client. >> >> Server is multi threaded to handle multiple client requests. >> >> All clients and server are running on the same system - localloop >> >> >> >> The problem i'm facing is that - >> >> Whatever the 1st client writes to the socket is not being read on the >> >> server side child process for that connection. >> >> For all the subsequent client - server connections , server is able to >> >> read and write back whatever is read. >> >> >> >> And one more info: >> >> >> >> I used wireshark to see what is happening - and connection is being >> >> established properly (3 way exchange) for all the clients. >> >> Also, the server is sending ACK for the 1st client's data, but this >> >> data is not reaching the server process connected with that client. >> >> For all subsequent client's the packet exchange is proper. >> >> >> >> Anything wrong I may be doing here? >> >> >> >> Thanks, >> >> Chandan >> >> -- >> >> To unsubscribe from this list: send the line "unsubscribe linux-net" in >> >> the body of a message to majordomo@xxxxxxxxxxxxxxx >> >> More majordomo info at http://vger.kernel.org/majordomo-info.html >> > >> > > >
#include "unp.h" int main(int argc,char *argv[]) { int sockfd,n; struct sockaddr_in servaddr; socklen_t servlen; if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) write_error("socket error"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERV_PORT); if( (n = inet_pton(AF_INET, argv[1], &servaddr.sin_addr)) <= 0) { if(n < 0) write_error("inet_pton failed"); else printf("inet_pton failed, no errno\n"); exit(1); } servlen = sizeof(servaddr); if( connect(sockfd,(struct sockaddr *)&servaddr,servlen) < 0) write_error("connect error"); str_cli(stdin,sockfd); exit(0); } int str_cli(FILE *fp,int sockfd) { char sendline[MAXLINE] , recvline[MAXLINE] , *ptr; size_t nleft = 0; ssize_t nwritten = 0; int i,rc; char c; printf("enter a string\n"); scanf("%s",sendline); ptr = sendline; nleft = strlen(sendline); while(nleft > 0) { if( (nwritten = write(sockfd,ptr,nleft)) <= 0 ) { if(errno == EINTR) nwritten = 0; else return -1; } nleft -= nwritten; ptr += nwritten; } printf("written %d bytes\n",nwritten); for(i = 0;i<100;i++) { if ( (rc = read(sockfd, &c , 1)) == 1) { recvline[i] = c; if (c == '\n') break; /* newline is stored, like fgets() */ } else if (rc == 0) { if (i == 1) return 0; /* EOF, no data read */ else break; /* EOF, some data was read */ } else return(-1); } recvline[++i] = '\0'; printf("Line received from socket : %s\n",recvline); } void write_error(char *err) { printf("write_error()\n"); /*char *errstr = strerror(errno); strcat(errstr,"\t"); strcat(errstr,err);*/ strcat(err,"\0"); printf("%s",err); exit(1); }
#include "unp.h" int main() { int listenfd, connfd; pid_t chpid; socklen_t clilen; struct sockaddr_in servaddr, cliaddr; listenfd = socket(AF_INET,SOCK_STREAM,0); if(listenfd < 0) { write_error("error while creating socket"); } bzero(&servaddr,sizeof(servaddr)); bzero(&cliaddr,sizeof(cliaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERV_PORT); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0) { write_error("error binding the socket"); } if(listen(listenfd,LISTENQ) < 0) { write_error("error listen()"); } //printf("server listening\n"); clilen = sizeof(cliaddr); printf("server to accept\n"); if(connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen) < 0) { exit(0); } str_echo(connfd); if(close(connfd) == -1) write_error("close failed"); } int str_echo(int sockfd) { int i, rc = 0; char c; char line[100],*ptr; size_t nleft; ssize_t nwritten; printf("inside str_echo\n"); //char temp[20]; for(i = 0;i<100;i++) { if ( (rc = read(sockfd, &c , 1)) == 1) { printf("read line : %c",c); line[i] = c; if (c == '\n') break; /* newline is stored, like fgets() */ } else if (rc == 0) { if (i == 1) return 0; /* EOF, no data read */ else break; /* EOF, some data was read */ } else return(-1); } line[++i] = '\0'; nleft = i; ptr = line; printf("Writing line to socket\n"); while(nleft > 0) { if( (nwritten = write(sockfd,ptr,nleft)) <= 0 ) { if(errno == EINTR) nwritten = 0; else return -1; } nleft -= nwritten; ptr += nwritten; } printf("written %d bytes",nwritten); } void write_error(char *err) { printf("write_error()\n"); /*char *errstr = strerror(errno); strcat(errstr,"\t"); strcat(errstr,err);*/ strcat(err,"\0"); printf("%s",err); exit(1); }
#include<sys/types.h> #include<sys/socket.h> #include<sys/time.h> #include<time.h> #include<netinet/in.h> #include<arpa/inet.h> #include<errno.h> #include<fcntl.h> #include<netdb.h> #include<signal.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/stat.h> #include<sys/uio.h> #include<unistd.h> #include<sys/wait.h> #include<sys/un.h> #ifdef HAVE_SYS_IOCTL_H # include <sys/ioctl.h> #endif #ifdef HAVE_SYS_FILIO_H # include <sys/filio.h> #endif #ifdef HAVE_SYS_SOCKIO_H # include <sys/sockio.h> #endif #ifdef HAVE_PTHREAD_H # include <pthread.h> #endif /* Define bzero() as a macro if it's not in standard C library. */ #ifndef HAVE_BZERO #define bzero(ptr,n) memset(ptr, 0, n) /* $$.If bzero$$ */ /* $$.If memset$$ */ #endif /* Following could be derived from SOMAXCONN in <sys/socket.h>, but many kernels still #define it as 5, while actually supporting many more */ #define LISTENQ 1024 /* 2nd argument to listen() */ /* Miscellaneous constants */ #define MAXLINE 4096 /* max text line length */ #define MAXSOCKADDR 128 /* max socket address structure size */ #define BUFFSIZE 8192 /* buffer size for reads and writes */ /* Define some port number that can be used for client-servers */ #define SERV_PORT 9877 /* TCP and UDP client-servers */ #define SERV_PORT_STR "9877" /* TCP and UDP client-servers */ #define UNIXSTR_PATH "/tmp/unix.str" /* Unix domain stream cli-serv */ #define UNIXDG_PATH "/tmp/unix.dg" /* Unix domain datagram cli-serv */ #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) /* default file access permissions for new files */ #define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH) /* default permissions for new directories */ typedef void Sigfunc(int); /* for signal handlers */ /* wrapper functions*/ void write_error(char *s); int str_echo(int sockfd); //used on server side //ssize_t Readline(int fd, void *ptr, size_t maxlen); //ssize_t readline(int fd,void *vptr, size_t maxlen); //void Writen(int fd,void *ptr,size_t bytes); //ssize_t writen(int fd, const void *ptr, size_t n); int str_cli(FILE *fp,int sockfd); //used on client side //void Fputs(const char *ptr, FILE *stream); //char * Fgets(char *ptr, int n, FILE *stream); //Sigfunc *Signal(int, Sigfunc *); //void sig_chld(int);