problem in writing a ftp server using socket
i'm writing a ftp server recently,and i now have a problem.
i use the client1 to connect the server,and it works well.but when the client2 connect the server,the server never receive the packet from client1.
when a new client come,a new client thread is created in ftp server with the new socket.
how can i solve the problem?
int main(){
pthread_t ServerThread;
void*thread_result;
int res;
res=pthread_create(&ServerThread,NULL,FTP_serverthread,NULL);
if(res!=0){
perror("server thread create error!");
exit(-1);
}
pthread_join(ServerThread,&thread_result);
return 0;
}
void *FTP_serverthread(void*pParam){
int sock=FTP_runserver();//this function complete the socket,bind,listen.
pthread_t clientthread;
void*thread_result;
int newsock;
char buffer[1024];
while(1){
int newsock=FTP_accept(sock,NULL);
struct sockaddr ClientAddress;
if(newsock==-1)
exit(-1);
else
printf("coming a new client!newsock is %d\n",newsock);
//if(checkuser(username,password)!=-1)
LockMutex(&mutex);
count++;
UnlockMutex(&mutex);
pthread_create(&clientthread,NULL,FTP_createnewclientthread,(void*)&newsock);
//printf("the thread num is %s\n",&clientthread);
}
pthread_join(clientthread,&thread_result);
pthread_exit(NULL);
}
void* FTP_createnewclientthread(void*pParam){
printf("a new client thread open!\n");
int* newsock=(int*)pParam;
char buffer[1024];
while(1){
if(recv(*newsock,&buffer,sizeof(buffer),MSG_DONTROUTE)<0)//it can work correctly when only one client,but not two or more!
perror("recv error!");
else
printf("recv S!\n");
printf("recieving from the new sock %d,buffer is %s\n",*newsock,buffer);
}
pthread_exit(NULL);
}
i'm new in C and linux.expecting your help.3q
|