逻辑很简单,该程序做两件事:
1) client 传送一消息给server,在server端收到此消息后将其打印出来
2) server端传送一列表给client, 该列表包括所有目前登录在此server(我的server名字叫myServer)上的user名,client端在收到此列表后将其打印出来。
对于步骤一,没有多少问题,关键是步骤二,不知从何下手。有个高手指点了一下,说可以用systm(“who|cut –fl –d’’>temp”) 把所有当前用户的名字放在一个叫temp的文件里,然后再读什么的。但我还是不明白怎么做。不知有哪位高手,能够详细指点一下。我把我的源代码放在这里,你可以在此基础上把步骤二的功能实现。
Server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>/* This program demonstrates the code for a simple server that operates
in a connection-oriented fashion.
The command to invoke it is
stream_server
It will wait for a process to connect to it and then wait to receive
a message from the other side of the connection. The message is
simply printed out at the server's site with no reply to the client
*/#define PORT 0x1501 /*some arbitrary port # */main()
{
int sock, length;
struct sockaddr_in server;
int msgsock;
char buf[1024];
int rval, i;
struct st {
char c[10];
int x;
} ;
struct st a;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket"); exit(1);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = 0; if (bind(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
perror("bind"); exit(1);
} length = sizeof server;
if (getsockname(sock, (struct sockaddr *) &server, &length) <0 ) {
perror("socket name");
exit(1);
} printf("SOCKET PORT# = %d \n", ntohs(server.sin_port)); listen(sock, 5); do {
msgsock = accept(sock, (struct sockaddr *) 0, (int *) 0); if (msgsock == -1)
perror("accept");
else do {
bzero(buf, sizeof(buf)); /* clean out the buffer */ /* in the next line, read(msgsock, buf, 1024) could have
been used in place of the call to recv
*/
if ((rval = recv(msgsock, buf, 1024, 0)) < 0)
perror("read"); if (rval != 0)
printf("--%s\n", buf);
} while (rval != 0);
close(msgsock);
} while (1);
exit(0);
}client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /*sleep */
#include <string.h>
/* This program demonstrates the code for a simple client that operates
in a connection-oriented fashion.
The command to invoke it is
stream_client server_host_name server_port#
After connecting with the server, it will send one message which
is printed on the server's machine.
*//* DATA is a test message sent by the client */
#define DATA "This is a test and only a test - don't worry?!"main(argc, argv)
int argc;
char *argv[];
{
int sock;
struct sockaddr_in server;
struct hostent *hp, *gethostbyname();
char buf[1024]; if (argc != 3) {
printf("usage: %s server_host_name server_port#\n", argv[0]);
exit(1);
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) {
perror("socket"); exit(1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp == 0) {
perror("gethostbyname"); exit(1);
}
bcopy((char *) hp->h_addr, (char *) &server.sin_addr,
hp->h_length);
server.sin_port = htons(atoi(argv[2]));
if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
perror("connect"); exit(1);
}
/* in the next line write(sock, DATA, sizeof(DATA)) could have been
used in place of the call to send
*/
if (send(sock, DATA, sizeof(DATA), 0) < 0) perror("write");
printf("data was sent to server\n");
shutdown(sock, 1); /* 2nd arg for shutdown
0 means no further input allowed
1 means no further output allwed
2 means the connection is shutdown in both directions
*/
close(sock);
exit(0);
}
1) client 传送一消息给server,在server端收到此消息后将其打印出来
2) server端传送一列表给client, 该列表包括所有目前登录在此server(我的server名字叫myServer)上的user名,client端在收到此列表后将其打印出来。
对于步骤一,没有多少问题,关键是步骤二,不知从何下手。有个高手指点了一下,说可以用systm(“who|cut –fl –d’’>temp”) 把所有当前用户的名字放在一个叫temp的文件里,然后再读什么的。但我还是不明白怎么做。不知有哪位高手,能够详细指点一下。我把我的源代码放在这里,你可以在此基础上把步骤二的功能实现。
Server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>/* This program demonstrates the code for a simple server that operates
in a connection-oriented fashion.
The command to invoke it is
stream_server
It will wait for a process to connect to it and then wait to receive
a message from the other side of the connection. The message is
simply printed out at the server's site with no reply to the client
*/#define PORT 0x1501 /*some arbitrary port # */main()
{
int sock, length;
struct sockaddr_in server;
int msgsock;
char buf[1024];
int rval, i;
struct st {
char c[10];
int x;
} ;
struct st a;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket"); exit(1);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = 0; if (bind(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
perror("bind"); exit(1);
} length = sizeof server;
if (getsockname(sock, (struct sockaddr *) &server, &length) <0 ) {
perror("socket name");
exit(1);
} printf("SOCKET PORT# = %d \n", ntohs(server.sin_port)); listen(sock, 5); do {
msgsock = accept(sock, (struct sockaddr *) 0, (int *) 0); if (msgsock == -1)
perror("accept");
else do {
bzero(buf, sizeof(buf)); /* clean out the buffer */ /* in the next line, read(msgsock, buf, 1024) could have
been used in place of the call to recv
*/
if ((rval = recv(msgsock, buf, 1024, 0)) < 0)
perror("read"); if (rval != 0)
printf("--%s\n", buf);
} while (rval != 0);
close(msgsock);
} while (1);
exit(0);
}client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /*sleep */
#include <string.h>
/* This program demonstrates the code for a simple client that operates
in a connection-oriented fashion.
The command to invoke it is
stream_client server_host_name server_port#
After connecting with the server, it will send one message which
is printed on the server's machine.
*//* DATA is a test message sent by the client */
#define DATA "This is a test and only a test - don't worry?!"main(argc, argv)
int argc;
char *argv[];
{
int sock;
struct sockaddr_in server;
struct hostent *hp, *gethostbyname();
char buf[1024]; if (argc != 3) {
printf("usage: %s server_host_name server_port#\n", argv[0]);
exit(1);
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) {
perror("socket"); exit(1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp == 0) {
perror("gethostbyname"); exit(1);
}
bcopy((char *) hp->h_addr, (char *) &server.sin_addr,
hp->h_length);
server.sin_port = htons(atoi(argv[2]));
if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
perror("connect"); exit(1);
}
/* in the next line write(sock, DATA, sizeof(DATA)) could have been
used in place of the call to send
*/
if (send(sock, DATA, sizeof(DATA), 0) < 0) perror("write");
printf("data was sent to server\n");
shutdown(sock, 1); /* 2nd arg for shutdown
0 means no further input allowed
1 means no further output allwed
2 means the connection is shutdown in both directions
*/
close(sock);
exit(0);
}
So, what on earth you want to ask about?
2) server端传送一列表给client, 该列表包括所有目前登录在此server(我的server名字叫myServer)上的user名,client端在收到此列表后将其打印出来。
对于步骤一,没有多少问题,关键是步骤二,不知从何下手。有个高手指点了一下,说可以用systm(“who|cut –fl –d’’>temp”) 把所有当前用户的名字放在一个叫temp的文件里,然后再读什么的。但我还是不明白怎么做。不知有哪位高手,能够详细指点一下。我把我的源代码放在这里,你可以在此基础上把步骤二的功能实现。