 |
| Linux General discussion of programming the various flavors of Linux operating systems. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Linux section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 27th, 2004, 04:35 AM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Problem in Socket Programming (BROKEN PIPE)
HI
I am getting error BROKEN PIPE in Linux C Sokcet programming.
i have written simple client-server program using socket.
pls help me.
The Code of programm is as follows.
/////////////////////////// server.c////////////////////////////////
main()
{
int server_sockfd,client_sockfd,server_len,client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
unlink("server_socket");
server_sockfd = socket(AF_INET,SOCK_STREAM,0);
server_address.sin_family =AF_INET;
server_address.sin_addr.s_addr=inet_addr("localhos t");
server_address.sin_port=9700;
server_len=sizeof(server_address);
bind(server_sockfd,(struct sockaddr *) &
server_address,server_len);
listen(server_sockfd,5);
while(1)
{
char ch;
printf("\n Server Waiting...\n");
client_len=sizeof(client_address);
client_sockfd=accept (server_sockfd,(struct sockaddr *) &
client_address,(socklen_t *)&client_len);
read(client_sockfd,&ch,1);
ch++;
write(client_sockfd,&ch,1);
close(client_sockfd);
}
}
////////////////////// client.c////////////////////////////////////
main()
{
int sockfd,len,result;
struct sockaddr_in address;
char ch='a';
sockfd=socket(AF_INET,SOCK_STREAM,0);
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("localhost");
address.sin_port=9734;
len=sizeof(address);
result=connect(sockfd,(struct sockaddr *) &address,len);
if(result==-1)
{
printf("Error Client");
}
write(sockfd,&ch,1);
read(sockfd,&ch,1);
printf("char from server = %c",ch);
close(sockfd);
}
__________________
Two things to be rememeber always......
(1) Never tell all that u know
|
|

April 12th, 2004, 08:12 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Ankur,
U seem to be a little confused with socket programming... in the connect system call u have to give the details regarding the server... hence u have to give the same port number as the one the server is listening to...
int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
A piece of advice to u... one basic idea of using sockets is to let 2 machines communicate with each other. Hence u shudnot use "localhost" to specify the IP address of the server... good practice wud be to send the server address as the argument to the client process or even better wud be to have a configuration file which contains the details of the server like the address and the port it is running on....
This code work now.... (note the changes made is only the port number at the client side)
/* Server program */
main()
{
int server_sockfd, client_sockfd, server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
// unlink("server_socket"); /* Don't kno y u've used this. so commented it :) */
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = 9700;
server_len = sizeof (server_address);
bind (server_sockfd, (struct sockaddr *) &server_address, server_len);
listen (server_sockfd, 5);
while (1) {
char ch;
printf ("\n Server Waiting...\n");
client_len = sizeof (client_address);
client_sockfd = accept (server_sockfd,
(struct sockaddr *) &client_address, (socklen_t *) &client_len);
read(client_sockfd,&ch,1);
ch++;
write(client_sockfd,&ch,1);
close(client_sockfd);
}
}
/* Client program */
main()
{
int sockfd, len;
struct sockaddr_in address;
char ch = 'a';
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; /* Give the server address here when running on 2 different machines */
address.sin_port = 9700;
len = sizeof(address);
if (connect (sockfd, (struct sockaddr *) &address, len) == -1) {
printf("Error Client");
}
write (sockfd, &ch, 1);
read (sockfd, &ch, 1);
printf ("char from server = %c", ch);
close (sockfd);
}
Hope this helps u out in ur problem,
Cherub
|
|
 |