- 论坛徽章:
- 0
|
我使用如下程序测试
服务器端:
// File: prg4_1.c
#include <stdio.h> /* These are the usual header files */
#include <strings.h> /* for bzero() */
#include <unistd.h> /* for close() */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#define PORT 1234 /* Port that will be opened */
#define BACKLOG 1 /* Number of allowed connections */
main()
{
int listenfd, connectfd; /* socket descriptors */
struct sockaddr_in server; /* server's address information */
struct sockaddr_in client; /* client's address information */
int sin_size;
/* Create TCP socket */
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
/* handle exception */
perror("Creating socket failed.");
exit(1);
}
/* set socket can be reused */
int opt = SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server,sizeof(server)); /* fill server with 0s */
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
/* handle exception */
perror("Bind error.");
exit(1);
}
if(listen(listenfd,BACKLOG) == -1){ /* calls listen() */
perror("listen() error\n");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1) {
/* calls accept() */
perror("accept() error\n");
exit(1);
}
printf("You got a connection from %s\n",inet_ntoa(client.sin_addr) ); /* prints client's IP */
send(connectfd,"Welcome to my server.\n",22,0); /* send to the client welcome message */
if(shutdown(connectfd,0)==-1)
{
perror("shutdown cfd 0");
}
if(shutdown(connectfd,0)==-1)
{
perror("shutdown cfd 0");
}
if(shutdown(connectfd,1)==-1)
{
perror("shutdown cfd 1");
}
if(shutdown(connectfd,1)==-1)
{
perror("shutdown cfd 1");
}
if(shutdown(connectfd,2)==-1)
{
perror("shutdown cfd 2");
}
if(shutdown(connectfd,2)==-1)
{
perror("shutdown cfd 2");
}
if(shutdown(listenfd,0)==-1)
{
perror("shutdown lfd 0");
}
if(shutdown(listenfd,0)==-1)
{
perror("shutdown lfd 0");
}
if(shutdown(listenfd,1)==-1)
{
perror("shutdown lfd 1");
}
if(shutdown(listenfd,1)==-1)
{
perror("shutdown lfd 1");
}
if (shutdown(listenfd,2) == -1)
{
perror("shutdown lfd 2");
}
if (shutdown(listenfd,2) == -1)
{
perror("shutdown lfd 2");
}
if (close(connectfd) == -1)
{
perror("close cfd");
}
if (close(connectfd) == -1)
{
perror("close cfd");
}
if (close(listenfd) == -1)
{
perror("close lfd");
}
if (close(listenfd) == -1)
{
perror("close lfd");
exit(1);
}
}
客户端
// File: prg4_2.c
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> /* netbd.h is needed for struct hostent */
#include <stdlib.h>
#define PORT 1234 /* Open Port on Remote Host */
#define MAXDATASIZE 100 /* Max number of bytes of data */
int main(int argc, char *argv[])
{
int fd, numbytes; /* files descriptors */
char buf[MAXDATASIZE]; /* buf will store received text */
struct hostent *he; /* structure that will get information about remote host */
struct sockaddr_in server; /* server's address information */
if (argc !=2) { /* this is used because our program will need one argument (IP) */
printf("Usage: %s <IP Address>\n",argv[0]);
exit(1);
}
if ((he=gethostbyname(argv[1]))==NULL){ /* calls gethostbyname() */
printf("gethostbyname() error\n");
exit(1);
}
if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){ /* calls socket() */
printf("socket() error\n");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT); /* htons() is needed again */
server.sin_addr = *((struct in_addr *)he->h_addr); /*he->h_addr passes "*he"'s info to "h_addr" */
if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){ /* calls connect() */
printf("connect() error\n");
exit(1);
}
if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){ /* calls recv() */
printf("recv() error\n");
exit(1);
}
buf[numbytes]='\0';
printf("Server Message: %s\n",buf); /* it prints server's welcome message */
close(fd); /* close fd */
}
运行结果:
[root@localhost home]# ./test3
shutdown cfd 1: Transport endpoint is not connected
shutdown cfd 2: Transport endpoint is not connected
shutdown cfd 2: Transport endpoint is not connected
shutdown lfd 0: Transport endpoint is not connected
shutdown lfd 1: Transport endpoint is not connected
shutdown lfd 1: Transport endpoint is not connected
shutdown lfd 2: Transport endpoint is not connected
shutdown lfd 2: Transport endpoint is not connected
close cfd: Bad file descriptor
close lfd: Bad file descriptor |
|