LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2009, 10:21 AM   #1
triff09
LQ Newbie
 
Registered: Dec 2009
Posts: 1

Rep: Reputation: 0
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
 
Old 12-23-2009, 01:49 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
You would probably do well to have an admin move this to the programming section and for readability sake please enclose your code in [ code ] tags so it keeps its formatting.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
ftp from LInux to Serv-U ftp server problem csross Linux - General 2 11-07-2005 04:35 PM
ftp writing problem 24jedi *BSD 1 06-20-2005 02:59 PM
socket binding and writing wahhie Programming 0 11-21-2004 08:38 PM
problem with writing into a file using socket(perl) akaash Programming 3 04-08-2004 06:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 02:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration