For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2004 > How can I run server and client applications in two different computers?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How can I run server and client applications in two different computers?
learning_C++

2004-09-27, 4:01 pm

Hi,
I found some example code about server and client applications. But I
can not run server and client in different computers. Now, I only
run them in the different windows in Linux. I hope to I can log on a
computer that run server application from a different computer. How
can I modify those codes?

Thanks,

/*server.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define MYPORT 3490 // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
void sigchld_handler(int s) {
while(wait(NULL) > 0);
}
int main(void) {
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector s address information
int sin_size;
struct sigaction sa;
int yes=1;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ perror("socket"); exit(1);
}
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEAD
DR,&yes,sizeof(int)) ==
-1) {
perror("setsockopt");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my
IP
memset(&(my_addr.sin_zero), '\0' , 8); // zero the rest of the
struct

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr)) == -1) {
perror("bind"); exit(1);
} if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}

while(1) { // main accept() loop
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,
&sin_size)) == -1) {
perror("accept");
continue; }
printf("server: got connection from %s\n",
inet_ntoa(their_addr.sin_addr));
if (!fork()) { // this is the child process
close(sockfd); // child doesn t need the listener
if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
} close(new_fd); // parent doesn t need this
} return 0;
}

/* ** client.c - a stream socket client demo */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 3490 // the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once
int main(int argc, char *argv[]) {
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector s address information
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0' , 8); // zero the rest of the
struct
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct
sockaddr)) == -1) {
perror("connect");
exit(1);
}
if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0' ;
printf("Received: %s",buf);
close(sockfd);
return 0;
}
learning_C++

2004-09-28, 4:00 am

Hi, Lew:
Thanks for your reply.
Can you give me an example to modify it? I have no experience on this
field now. But I hope to learn through the example.

Lew Pitcher <Lew.Pitcher@td.com> wrote in message news:<9EZ5d.2694$tT2.423985@news20.bellglobal.com>...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> learning_C++ wrote:
> [code snipped]
>
> Your code, compiled verbatum on two of my systems, works just fine for me. I ran
> your server code on my server station, and your client code on my laptop.
>
> FWIW, client code should be invoked with the name or IP address of the server.
>
> - --
> Lew Pitcher
> IT Consultant, Enterprise Data Systems,
> Enterprise Technology Solutions, TD Bank Financial Group
>
> (Opinions expressed are my own, not my employers')
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (MingW32)
>
> iD8DBQFBWGaPagVFX4UWr64RAmrfAKCtCzCQzWoU
uQnvNrVMjO6thlm4CACg7YQI
> seP7Svyj/7+oGB8QQmiLTFA=
> =iVyr
> -----END PGP SIGNATURE-----

learning_C++

2004-09-28, 4:12 pm

Hi, I run the client and server program in the computer with the host
name "senilix.hut.fi". I open two windows. one is runing the client
application and the other is runing the server application. The
execution is sucessful.
I can get the following information:
server: got connection from 130.233.248.203
Received: Hello, world!

When I run the client application in "senilix.hut.fi" and server
application in "idefix.hut.fi".
I only can get the information on client side:
connect: Connection refused
Please help me.

Thanks,

Lew Pitcher <Lew.Pitcher@td.com> wrote in message news:<XFc6d.5473$tT2.672393@news20.bellglobal.com>...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> learning_C++ wrote:
>
> You're welcome
>
>
> Modify it how? FWIW, I didn't modify the programs at all and managed to
> get them to run and communicate between different systems. As far as I
> can tell, they don't require modification.
>
> Perhaps, instead of modifying the programs, you can show us what you do
> and why you think it's failing when you run the programs across a network.
>
> - --
>
> Lew Pitcher, IT Consultant, Enterprise Data Systems
> Enterprise Technology Solutions, TD Bank Financial Group
>
> (Opinions expressed here are my own, not my employer's)
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (MingW32)
>
> iD8DBQFBWVb0agVFX4UWr64RAvbUAJ4qAjSg3wib
QMFgLUo/iq4xpG9yUACg4NJz
> pKQA6C3qh13np3I3fwnRB8w=
> =tGuC
> -----END PGP SIGNATURE-----

Jens.Toerring@physik.fu-berlin.de

2004-09-28, 9:28 pm

learning_C++ <learning_c@hotmail.com> wrote:
> Hi, I run the client and server program in the computer with the host
> name "senilix.hut.fi". I open two windows. one is runing the client
> application and the other is runing the server application. The
> execution is sucessful.
> I can get the following information:
> server: got connection from 130.233.248.203
> Received: Hello, world!


> When I run the client application in "senilix.hut.fi" and server
> application in "idefix.hut.fi".
> I only can get the information on client side:
> connect: Connection refused


Is there a firewall running on the server or in between the server and
the client machine, blocking connections to the port you try to connect
to?
Regards, Jens

PS. If you reply to someone please don't top-most, i.e. always put
your reply below what you're answering to and remove all text from
previous posts that aren't relevant to what you write.
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com