|
-
Sep 18th, 2001, 11:59 AM
#1
Thread Starter
Member
client-server programs
i am learning to write UNIX socket and client programs.
i wrote the UNIX socket server program, this program uses a socket to wait for any incoming messages and then displays them on screen. i use the server ports 5010 to 5019.
and i compile server.c with the command "gcc server.c -o server -lsocket -lnsl".
i also wrote the UNIX socket client program, client.c that will send messages to the server program. i compile the program using gcc compiler; "gcc client.c -o client -lsocket -lnsl"
i tested the programs using two telnet sessions (one for server program and other for client program) at my PC. ( both telnet sessions r logged to the remote UNIX system and i just run both programs there.)
my problem is, the server does not display messages on screen when the client send messages to this server. ( i don't know where is the problem)
how can i modify both programs such that the server sends a reply message to the client upon receipt of a client's message??
below is the client.c and server.c
Code:
//client.c; usage: client port_number host_name. application:client process sends packets to server process
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#define bcopy(b1, b2, len) memmove(b1, b2, (size_t)(len))
#define bzero(b, len) memset(b, 0, (size_t)(len))
#define bcmp(b1,b2,len) memcmp(b1, b2, (size_t)(len))
#define bufsize 128
int sc, s1, len;
struct hostent*hp;
struct sockaddr_in tosever;
main(argc, argv)
int argc; char*argv[];
{
char buf[bufsize];
sc = socket(AF_INET, SOCK_STREAM, 0);
bzero((char*)&tosever, sizeof(tosever));
tosever.sin_family = AF_INET;
tosever.sin_port = htons(atoi(argv[1]));
if ((hp=gethostbyname(argv[2]))==NULL){
fprintf(stderr, "%s- unknown host.\n", argv[2]);
exit(-1);
}
bcopy(hp->h_addr, (char*)&tosever.sin_addr, hp->h_length);
s1 = connect(sc,(struct sockaddr*)&tosever, sizeof(tosever));
printf("Enter your message: \n");
len = read(0, buf, bufsize);
s1=send(sc,buf,len,0);
close(sc);
exit(0);
}
//--------------------------------------------------------------------------
//server.c; usage:server port_number where port_num is greater than 1023. Application: server listens to a specific port. when a msg is receives, server displays it on screen and exits.
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#define bcopy(b1, b2, len) memmove(b1, b2, (size_t)(len))
#define bzero (b, len) memset(b, 0,size_t)(len))
#define bcmp (b1, b2, len) memcmp(b1, b2, (size_t)(len))
#define bufsize 128
main(argc, argv)
int argc; char*argv[];
{
int snew, ss, len, lenc;
char buf[bufsize];
struct sockaddr_in name, cli_addr;
name.sin_family = AF_INET;
name.sin_addr.s_addr = INADDR_ANY;
name.sin_port = htons(atoi(argv[1]));
ss = socket(AF_INET, SOCK_STREAM, 0);
bind(ss, (struct sockaddr*)&name, sizeof(name));
listen(ss,1);
lenc = sizeof(cli_addr);
snew = accept(ss, (struct sockaddr*)&cli_addr, &lenc);
len = recv(snew, buf, sizeof(buf), 0);
buf[len] = NULL;
printf("Message received....%s\n", buf);
close(ss);
exit(0);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|