LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2004, 05:58 AM   #1
ljqu_happy
LQ Newbie
 
Registered: Aug 2004
Posts: 18

Rep: Reputation: 0
socket program:why server cann't receive


I wrote a simple socket program in C, but met an question:my server program cann't receive data from the client program, though the client program can receive data from the server program. Why? I went back to many books and many webpages of the Internet, but cann't find the reason.
Who can tell me the reason,thanks
Below is the program.
 
Old 10-30-2004, 06:01 AM   #2
ljqu_happy
LQ Newbie
 
Registered: Aug 2004
Posts: 18

Original Poster
Rep: Reputation: 0
/******* server.c ************/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size,portnumber, recv_len;
char buffer[1024];

if(argc!=2)
{
fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
exit(1);
}

if((portnumber=atoi(argv[1]))<0)
{
fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
exit(1);
}

/* build socke */
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
exit(1);
}

/* fill sockaddr struct */
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(portnumber);

/* bind sockfd */
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))== -1)
{
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}

/*listen socket */
if(listen(sockfd,5)==-1)
{
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
while(1)
{
/*wait for connect*/
sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),(socklen_t *)&sin_size ))==-1)
{
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit(1);
}
fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));

recv_len = -1;
while(recv_len == -1)
{
recv_len=recv(sockfd,buffer,1024, 0);
if(recv_len != -1)
{
buffer[recv_len]='\0';
printf("received:%s\n",buffer);
}
}
/* the communication is end */
shutdown(new_fd, SHUT_RD);
/* for the next */
}
shutdown(sockfd, SHUT_RD);
exit(0);
}

Last edited by ljqu_happy; 10-30-2004 at 06:12 AM.
 
Old 10-30-2004, 06:03 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Can you use the code tag ? Indentation would ease reading your program.
 
Old 10-30-2004, 06:04 AM   #4
ljqu_happy
LQ Newbie
 
Registered: Aug 2004
Posts: 18

Original Poster
Rep: Reputation: 0
/******* client.c ************/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
int sockfd;
char hello[]="I'm ljqu_happy.\n";
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes, recv_len;

if(argc!=3)
{
fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
exit(1);
}

if((host=gethostbyname(argv[1]))==NULL)
{
fprintf(stderr,"Gethostname error\n");
exit(1);
}

if((portnumber=atoi(argv[2]))<0)
{
fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
exit(1);
}

/* build socket */
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
exit(1);
}

/* fill socket */
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);

/* connect to server */
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)
)==-1)
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}

/* connect successful */
if((send(sockfd, hello, sizeof(hello), 0))==-1)
{
fprintf(stderr,"send name Error:%s\n",strerror(errno));
exit(1);
}
printf("send data %s OK!\n", hello);

/*communication is end */
shutdown(sockfd, SHUT_RD);
exit(0);
}

Last edited by ljqu_happy; 10-30-2004 at 06:15 AM.
 
Old 10-30-2004, 06:42 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
No improvement in your code readability, but anyway you use the wrong file descriptor in your recv call, it should be new_fd.
 
Old 10-30-2004, 06:49 AM   #6
dmigh
LQ Newbie
 
Registered: Oct 2004
Posts: 29

Rep: Reputation: 15
why this forum doesn't have file attachment function?
Is there special reason about it?

--
 
Old 10-30-2004, 07:20 AM   #7
ljqu_happy
LQ Newbie
 
Registered: Aug 2004
Posts: 18

Original Poster
Rep: Reputation: 0
Jlliagre is wellcome You gave the right answer.Thank you very much.But I'm very sorry for asking such a simple question,I thought things too complicated, and didn't just sit down to look carefully at my code.I should remember this lesson.
BTW, I have indented the code, but the web don't support the space at the head of the line, there only left space line.
 
Old 10-30-2004, 08:26 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
As I asked you before, put your code between code tags, like this
Code:
this is:
    indented !
 
  


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
Unable to receive data in an udp socket stephenwalter Programming 8 11-24-2005 11:20 PM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
creating server socket program to handle multiple client at same time cranium2004 Programming 2 03-14-2005 10:58 AM
What program use to receive RTP audio or video that compatible with JMF vanhelsing Programming 0 08-05-2004 10:58 AM
How to receive UDP and ICMP packets, by one UDP socket(PMTUD) myself_rajat Linux - Networking 0 05-28-2004 05:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 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