LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-14-2010, 01:54 AM   #1
zak100
Member
 
Registered: Jul 2009
Posts: 262

Rep: Reputation: 2
Prob: Client/Server Communication


Hi,
I have got a client/server program from a socket programming tutorial on the net. When I am executing it as a localhost(client/server on same machine), its working fine but I am working on different machines, its not working. Can somebody plz guide me with this???
Cleint code is
Code:
//echo client

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>

#define  BUFFSIZE 32

void Die (char *mess){perror (mess); exit(1);}

main(int argc, char *argv[ ]){

int sock;
struct  sockaddr_in  echoserver;
char  buffer[BUFFSIZE];
unsigned int echolen;
int received=0;

    if(argc!=4){
	fprintf(stderr, "USAGE: TCPecho  <server_ip> <word> <port>\n");
	exit(1);
    }
    if((sock= socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
	Die ("Failed to create socket");
    }
    memset(&echoserver, 0, sizeof(echoserver));
    echoserver.sin_family = AF_INET;
    echoserver.sin_addr.s_addr = inet_addr(argv[1]);
    echoserver.sin_port=htons(atoi(argv[3]));
    if(connect(sock, (struct sockaddr * ) &echoserver, sizeof (echoserver)) <0) {
       Die("Failed to connect with server");
    }
    /* send the word to server */
    echolen= strlen(argv[2]);
    if(send(sock, argv[2], echolen, 0) != echolen) {
       Die("Mismatch in number of sent bytes");
    }
    /* Receive the word back from the server */
    fprintf(stdout , "Received");
    while(received < echolen) {
    int bytes =0;
       if((bytes= recv(sock, buffer, BUFFSIZE-1, 0)) < 1) {
          Die("Failed to receive bytes from server");
       }
    received += bytes;
    buffer[bytes]='\0';
    fprintf(stdout, buffer);
    }
    fprintf(stdout, "\n");
    close (sock);
    exit(0);
}
and server code is:
Code:
//echo  server
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>

#define  BUFFSIZE 32
#define MAXPENDING 5 

void Die (char *mess){
	perror (mess); 
	exit(1);
}


void HandleClient(int sock) {
	char buffer[BUFFSIZE];
	int received = -1;
	if((received=recv(sock, buffer, BUFFSIZE, 0)) < 0) {
	   Die("Failed to receive message ");
	}
	while (received > 0) {
		if (send (sock, buffer, received, 0) != received) {
			Die("Failed to send bytes to Client");
		}
		if((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) {
			Die("Failed to receive additional bytes from client");
		}
	}
	close(sock);
}


main(int argc, char *argv[ ]){	
int serversock, clientsock;
struct  sockaddr_in  echoserver, echoclient;
if(argc !=2) {
	fprintf(stderr, "USAGE: echoserver <port>\n");
        exit(1);
}
if((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
	Die("Failed to create socket");
}

memset(&echoserver, 0, sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr = htonl (INADDR_ANY);
echoserver.sin_port=htons(atoi(argv[1]));
if(bind(serversock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0) {
	Die("Failed to bind the server socket");
}
if(listen(serversock, MAXPENDING) < 0) {
	Die("Failed to listen on server socket");
}
while(1) {
	unsigned int clientlen= sizeof(echoclient);
	if((clientsock=accept(serversock, (struct sockaddr *) &echoclient, &clientlen)) < 0) {
		Die ("Failed to accept client connection");

	}
	fprintf(stdout, "Client connected: %s\n", inet_ntoa(echoclient.sin_addr));
	HandleClient(clientsock);
}

}
On same machines, works well with both localhost address and IP address of machine:
(server execution)
$./sock2serv 2000
Client connected: 172.16.21.70
Received from client:babar
(client execution)
$./sock2cli 172.16.21.70 babar 2000
received babar
$

Somebody plz guide with this problem. I am getting :
Code:
Failed to connect with server: No route to host
However if I am doing the ping from the remote machine, its working. Kindly help me with this.

Zulfi.

Last edited by zak100; 10-14-2010 at 01:57 AM.
 
Old 10-14-2010, 02:09 AM   #2
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: RHEL/Ubuntu/Debian/Fedora/Centos/K3OS
Posts: 1,159
Blog Entries: 4

Rep: Reputation: 149Reputation: 149
Hi,

Disable iptables and selinux.
 
Old 10-18-2010, 12:14 PM   #3
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Hi,
Thanks for your guidance. I am able to disable these services using administrative tools on Fedora.

Zulfi.
 
  


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
Server/Client communication using VM( specifically VirtualBox) Rawan Alhindawi Linux - Newbie 1 12-04-2009 07:22 AM
Server Client communication Kakarot_Rathish Programming 7 01-09-2009 05:40 PM
Microsoft Live communication server 2005 client for linux? alem2k5 Linux - Enterprise 9 07-24-2008 12:24 AM
Communication btw Nessus Server & Client ravikumarv Programming 5 11-23-2007 11:18 PM
client server communication suing raw socket revanth Linux - Networking 1 03-13-2007 09:52 PM

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

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