服务器代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3490
#define MAXLINE 100
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MAXLINE 100
#define MAX(a,b) (((a)>(b))?(a):(b))
void sig_chld(int signo)
{
pid_t pid;
int stat;
while ( (pid= waitpid(-1,&stat,WNOHANG)) > 0)
#ifdef DEBUG
printf("Child %d terminated\n",pid);
#endif
return;
}
#ifdef DEBUG
printf("Child %d terminated\n",pid);
#endif
return;
}
void str_echo(int sockfd)
{
ssize_t n;
char line[MAXLINE];
fd_set rfds;
int retval;
struct timeval tv;
for (;;) {
memset(line,0,sizeof(line));
FD_ZERO(&rfds);
FD_SET(sockfd, &rfds);
tv.tv_sec = 60;
tv.tv_usec = 0;
retval = select(sockfd+1, &rfds, NULL, NULL, &tv);
memset(line,0,sizeof(line));
FD_ZERO(&rfds);
FD_SET(sockfd, &rfds);
tv.tv_sec = 60;
tv.tv_usec = 0;
retval = select(sockfd+1, &rfds, NULL, NULL, &tv);
if (retval) {
#ifdef DEBUG
printf("Data is available now.\n");
#endif
} else {
#ifdef DEBUG
printf("No data within five seconds.\n");
#endif
break;
}
}
}
#ifdef DEBUG
printf("Data is available now.\n");
#endif
} else {
#ifdef DEBUG
printf("No data within five seconds.\n");
#endif
break;
}
}
}
int main(int argc, char **argv)
{
int listenfd, connfd, udpfd, nready, maxfdp1;
char mesg[MAXLINE];
pid_t childpid;
fd_set rset;
ssize_t n;
socklen_t len;
const int on = 1;
int i=0;
struct sockaddr_in cliaddr, servaddr;
void sig_chld(int);
udpfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
bind(udpfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
printf("The server is waiting datas:\n");
signal(SIGCHLD, sig_chld);
{
int listenfd, connfd, udpfd, nready, maxfdp1;
char mesg[MAXLINE];
pid_t childpid;
fd_set rset;
ssize_t n;
socklen_t len;
const int on = 1;
int i=0;
struct sockaddr_in cliaddr, servaddr;
void sig_chld(int);
udpfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
bind(udpfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
printf("The server is waiting datas:\n");
signal(SIGCHLD, sig_chld);
maxfdp1 = MAX(listenfd, udpfd) + 1;
while(1) {
FD_ZERO(&rset);
listenfd =0;
FD_SET(listenfd, &rset);
FD_SET(udpfd, &rset);
nready = select(maxfdp1, &rset, NULL, NULL, NULL);
if (nready < 0) {
if (errno == EINTR) continue;
else perror("select error");
}
FD_ZERO(&rset);
listenfd =0;
FD_SET(listenfd, &rset);
FD_SET(udpfd, &rset);
nready = select(maxfdp1, &rset, NULL, NULL, NULL);
if (nready < 0) {
if (errno == EINTR) continue;
else perror("select error");
}
if (FD_ISSET(listenfd, &rset)) {
len = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &len);
if ( (childpid = fork()) == 0) {
close(listenfd);
str_echo(connfd);
exit(0);
}
close(connfd);
}
if (FD_ISSET(udpfd, &rset)) {
len = sizeof(cliaddr);
len = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &len);
if ( (childpid = fork()) == 0) {
close(listenfd);
str_echo(connfd);
exit(0);
}
close(connfd);
}
if (FD_ISSET(udpfd, &rset)) {
len = sizeof(cliaddr);
n = recvfrom(udpfd, mesg, MAXLINE, 0, (struct sockaddr *) &cliaddr, &len);
i++;
printf("Client %d contains:%s\n",i,mesg);
}
}
}
i++;
printf("Client %d contains:%s\n",i,mesg);
}
}
}
客户端代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3490
#define MAXDATASIZE 100
#define MAXDATASIZE 100
int main(int argc, char * argv[])
{
int sockfd,numbytes,sin_size;
char buf[MAXDATASIZE] = "hello world!";
struct hostent *he;
struct sockaddr_in their_addr;
{
int sockfd,numbytes,sin_size;
char buf[MAXDATASIZE] = "hello world!";
struct hostent *he;
struct sockaddr_in their_addr;
if(argc != 2)
{
fprintf(stderr,"usage:client hostname\n");
exit(1);
}
{
fprintf(stderr,"usage:client hostname\n");
exit(1);
}
if((he = gethostbyname(argv[1])) == NULL)
{
herror("gethostbyname");
exit(1);
}
{
herror("gethostbyname");
exit(1);
}
if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("socket");
exit(1);
}
{
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he -> h_addr);
bzero(&(their_addr.sin_zero),8);
numbytes = sendto(sockfd,buf,MAXDATASIZE,0,(struct sockaddr *)&their_addr,sizeof(their_addr));
if(numbytes == -1)
{
perror("sendto");
exit(1);
}
printf("send:%s\n",buf);
close(sockfd);
return 0;
}
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he -> h_addr);
bzero(&(their_addr.sin_zero),8);
numbytes = sendto(sockfd,buf,MAXDATASIZE,0,(struct sockaddr *)&their_addr,sizeof(their_addr));
if(numbytes == -1)
{
perror("sendto");
exit(1);
}
printf("send:%s\n",buf);
close(sockfd);
return 0;
}