LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-16-2008, 04:21 AM   #1
sweetytweety
LQ Newbie
 
Registered: May 2008
Posts: 17

Rep: Reputation: 0
ERROR reading from socket: Connection reset by peer


hi,
i try to communicate two client with the server(server recveies message send by one client,in another client while running got the message "ERROR reading from socket: Connection reset by peer
". can to correct
 
Old 05-16-2008, 04:44 AM   #2
digvijay.gahlot
Member
 
Registered: Mar 2008
Posts: 53

Rep: Reputation: 15
Can you explain your problem in detail. Give some sample code for clearity
 
Old 05-16-2008, 05:47 AM   #3
sweetytweety
LQ Newbie
 
Registered: May 2008
Posts: 17

Original Poster
Rep: Reputation: 0
Client.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc,char *argv[])
{
int sockfd,portno,n;

FILE *fptr;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];

sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
error("ERROR opening socket");

server=gethostbyname("10.128.18.251");
if(server==NULL)
{
fprintf(stderr,"ERROR,no such host\n");
exit(0);
}
bzero((char*)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
bcopy((char*)server->h_addr,(char*)&serv_addr.sin_addr.s_addr,server->h_length);

serv_addr.sin_port=htons(7000);
if(connect(sockfd,&serv_addr,sizeof(serv_addr))<0)
error("ERROR connecting");
printf("please Enter the message :");
bzero(buffer,256);
while(1){
fgets(buffer,255,stdin);
n=write(sockfd,buffer,strlen(buffer));
if(n<0)
error("ERROR writing to socket");
bzero(buffer,256);
n=read(sockfd,buffer,255);
if(n<0)
error("ERROR reading from socket");
printf("%s\n",buffer);
}
close(sockfd);

return 0;
}


Server.c
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

void error(char *msg)
{
perror(msg);
exit(1);
}

int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,clilen,n,i;
char buffer[256];
int optval=1;
struct sockaddr_in serv_addr,cli_addr;

sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
error("ERROR opening socket");
bzero((char *)&serv_addr,sizeof(serv_addr));

serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof optval);
serv_addr.sin_port=htons(7000);
if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
error("ERROR on binding");
//while(1)

n=listen(sockfd,5);
if(n==-1)
error("Error :listen");
clilen=sizeof(cli_addr);


newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
if(newsockfd<0)
error("ERROR on accept");
bzero(buffer,256);


n=read(newsockfd,buffer,255);
if(n<0)
error("ERROR reading from socket");
printf("Here is the message:%s",buffer);
n=write(newsockfd, "got your message",18);
if(n<0)
error("ERROR writing to socket");
close(sockfd);

return 0;

.........................................
Above i ve given my cde i want to communicate two clients to one server. whether i ve to modify anything in my code?
 
Old 05-16-2008, 06:21 AM   #4
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
After accepting the first client, your server continue until its end...
You have to do a loop with the accept inside, but with such an arch, you wont be able to manage more than one client at once. So it's better to do something like :
Code:
while (continue_to_accept)
   accept
   create a new thread or process for the new client
end
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
FreeNX help. When I try to connect I get error msg (connection reset by peer) MegaSvensk Linux - Software 1 10-18-2007 03:09 PM
SSH fatal: Read from socket failed: Connection reset by peer humbletech99 Linux - Networking 3 01-03-2007 07:41 PM
welcome response error + connection reset by peer rnj Linux - Software 0 10-20-2004 11:04 AM
Network error: Connection reset by peer jeffpoulsen Linux - Networking 0 10-09-2003 07:31 PM
Network Error: Connection Reset by Peer Bob Welch Linux - Networking 3 12-09-2002 02:07 PM


All times are GMT -5. The time now is 05:54 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration