LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-26-2005, 04:27 AM   #1
James_dean
Member
 
Registered: Sep 2005
Posts: 41

Rep: Reputation: 15
Client/Server connection problem


i have two computers connected onto a network here at work. I want to run my server.c on one computer and get a response from client.c. This works fine with localhost and using two terminal screens.........one for the server and other for the client. But it does not work if i run the server program on another computer. How can i fix this.....what are the steps involved in doing this. Here are my programs(very simple).

/*server.c*/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int port = 8000;

void main(int argc,char *argv[])
{
struct sockaddr_in pin;
struct sockaddr_in sin;
int sock_descriptor;
int temp_sock_descriptor;
int address_size;
char buf[16384];
int i, len;

sock_descriptor = socket(AF_INET,SOCK_STREAM,0);
if(sock_descriptor == -1){
perror("call to socket");
//exit(1);
}
//char *str = "A default test string";

bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);

if(bind(sock_descriptor,(struct sockaddr *)&sin, sizeof(sin)) == -1){
perror("call to bind\n");
//exit(1);
}

if(listen(sock_descriptor,20) == -1){
perror("call to listen\n");
//exit(1);
}

printf("Accepting connections.....\n");

while(1){
temp_sock_descriptor =
accept(sock_descriptor, (struct sockaddr*)&pin,
&address_size);
if(temp_sock_descriptor == -1){
perror("call to accept");
//exit(1);
}
if(recv(temp_sock_descriptor,buf,16384,0) == -1){
perror("call to recv");
//exit(1);
}

printf("Received from client:%s\n",buf);

len = strlen(buf);
for(i = 0;i < len;i++) buf[i] = toupper(buf[i]);

if(send(temp_sock_descriptor, buf, len, 0) == -1){
perror("call to send");
exit(1);
}
close(temp_sock_descriptor);
}
}
/*client.c*/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

char* host_name = "xxx.16.13.124";
int port = 8000;

int main(int argc,char *argv[])
{
char buf[8192];
char message[256];
int socket_descriptor;
struct sockaddr_in pin;
struct hostent *server_host_name;

char *str = "A default test string";

if(argc < 2){
printf("Usage:'test \"Any test string\"\n");
printf("We will send a default test string.\n");
}
else{
str = argv[1];
}
if((server_host_name = gethostbyname(host_name)) == 0){
perror("Error resolving local host\n");
}

bzero(&pin,sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = htonl(INADDR_ANY);
pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
pin.sin_port = htons(port);

if((socket_descriptor = socket(AF_INET, SOCK_STREAM,0)) == -1){
perror("Error opening socket\n");
exit(1);
}
if(connect(socket_descriptor,(void*)&pin, sizeof(pin)) == -1){
perror("Error connecting to socket\n");
exit(1);
}
printf("Sending message %s to server.....\n",str);

if(send(socket_descriptor,str,strlen(str),0) == -1){
perror("Error in Send\n");
exit(1);
}

printf("...sent message..wait for response..\n");

if(recv(socket_descriptor,buf,8192,0) == -1){
perror("Error in receiving response from server\n");
exit(1);
}

printf("\nResponse from server:\n\n%s\n",buf);

close(socket_descriptor);
exit(0);
}
 
Old 09-26-2005, 05:49 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Error messages?

Your client connects to a hardcoded IP address. Is that the address of the computer where the server app runs?
Maybe a firewall that blocks access to port 8000? Can you telnet to the server-PC on port 8000 (telnet hostaddr 8000)?

Can you ping the server PC from the client PC?

What is that xxx in host_name in the client app? (just hiding details for us?)
 
Old 09-26-2005, 08:02 PM   #3
James_dean
Member
 
Registered: Sep 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Error message is the same as for the telnet. It is "No route to host"............yes the xxx is just to hide data.......
 
Old 09-26-2005, 08:03 PM   #4
James_dean
Member
 
Registered: Sep 2005
Posts: 41

Original Poster
Rep: Reputation: 15
I can ping the pc but not telnet it.........
 
Old 09-26-2005, 08:57 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try telnet with ip address, then try telnet with FQDN. It sounds like it can't find the other box ie DNS issue or possibly Firewall.
 
Old 09-27-2005, 09:00 PM   #6
James_dean
Member
 
Registered: Sep 2005
Posts: 41

Original Poster
Rep: Reputation: 15
I can telnet my default gateway........but not this other pc. I tried to run the telnet server service on the other pc but it got stopped for some reason and wouldn't allow it.
 
Old 09-28-2005, 07:54 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
run your server app on the second PC (and the client as well);
1)
Use localhost
Works? if yes, next step
2)
Use ip-address xxx.16.13.124 (server and client still running on same PC)
Works? if yes, next step
3)
Use the 2 pc's, one running client and other running server
We know that the one way does not work (client on PC1, server on PC2);
does the other way work?

Still guess you have a firewall issue that blocks access to the port.

BTW, you don't need a telnet server to test what I suggested earlier on. You 'simply' telnet into the server app (telnet localhost 8000 and telnet xxx.16.13.124 8000). Your server app must obviously be running.

Last edited by Wim Sturkenboom; 09-29-2005 at 01:33 AM.
 
Old 09-28-2005, 08:31 PM   #8
James_dean
Member
 
Registered: Sep 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks alot for your responses. You have been very helpful. I should be able to resolve this issue now. I am quite sure that you are right. I done the telnet in both directions but no luck and i used the same syntax you used above
 
  


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
LDAP SERVER Client Connection on SUSE 9.2 - connection error jcarton Linux - Networking 3 03-19-2005 12:40 PM
LDAP SERVER - CLIENT CONNECTION on suse 9.2 - connection error nicolasdiogo Linux - Networking 4 03-01-2005 01:43 PM
LDAP SERVER - CLIENT CONNECTION on suse 9.2 - connection error nicolasdiogo SUSE / openSUSE 0 03-01-2005 05:43 AM
DHCP Server Client no connection to Internet westverg Linux - Networking 3 02-28-2005 07:08 AM
Showing CLIENT<-server->INTERNET connection from server. druuna Linux - Networking 2 05-03-2004 01:33 PM

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

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