程序内容:服务端程序
运行环境:hp unix 操作系统上测试通过
编译命令:cc -o server server.c
执行命令:./server
源代码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define MYPROT 3490 /*服务器监听端口号*/
#define BACKLOG 10 /*最大同时连接请求数*/
/***************相关说明***********************************
SOCK_STREAM表示面向连接TCP,SOCK_DGRAM表示无连接UDP
struct sockaddr
{
unsigned short sa_family; *地址族, AF_xxx*
char sa_data[14]; *14字节的协议地址*
}
struct sockaddr_in
{
short int sin_familu; *地址族*
unsigned short int sin_prot;*端口号*
struct in_addr sin_addr; *IP地址*
unsigned char sin_zero[8]; *填充0以保持与struct sockaddr同样的大小*
}
**********************************************************/
main()
{
int sockfd,new_fd; /*监听socket: sockfd,数据传输socket: new_fd*/
struct sockaddr_in my_addr; /*本机地址信息*/
struct sockaddr_in their_addr; /*客户地址信息*/
int sin_size;
if ( -1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0)) )
{ /*错误检测*/
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPROT); /*如果等于0则随机选择一个端口*/
my_addr.sin_addr.s_addr = INADDR_ANY; /*系统填入本机IP地址*/
bzero(&(my_addr.sin_zero),8); /*初始化my_addr.sin_zero为0*/
/*绑定端口*/
if ( -1 == bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)))
{ /*错误检测*/
perror("bind");
exit(1);
}
/*监听端口*/
if ( listen(sockfd, BACKLOG) == -1 )
{ /*错误检测*/
perror("listen");
exit(1);
}
while(1) /*main accept() loop*/
{
sin_size = sizeof(struct sockaddr_in);
/*接受连接请求,并且建立一个新的Socket描述符*/
new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size);
if ( -1 == new_fd )
{
perror("accept");
continue;
}
printf("server:got connection form-%s",inet_ntoa(their_addr.sin_addr));
/*子进程代码段*/
if ( !fork() )
{
/*如果是使用的UDP,发送数据使用sendto,接收用recvfrom*/
if ( -1 == send(new_fd,"Hello,world!",14,0) )
{
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); /*父进程不再需要该socket,关闭使用shutdown*/
waitpid(-1,NULL,WNOHANG); /*等待子进程结束,清除子进程所占用资源*/
}
}
}
程序内容:客户端程序
运行环境:hp unix 操作系统上测试通过
编译命令:cc -o client client.c
执行命令:./client IP地址(host)
源代码:
#include <stdio.h>
#include <stdlib.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
#define MAXDATASIZE 100 /*每次最大数据传输量*/
int main(int argc, char *argv[])
{
int sockfd,numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr;
if (argc != 2)
{
fprintf(stderr,"usage: client hostname/n");
exit(1);
}
if ( (he = gethostbyname(argv[1])) == NULL )
{
perror("gethostbyname");
exit(1);
}
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -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);
/*建立连接*/
if ( connect( sockfd, (struct sockaddr *)&their_addr,
sizeof(struct sockaddr) ) == -1 )
{
perror("connect");
exit(1);
}
if ( (numbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1 )
{
perror("recv");
exit(1);
}
buf[numbytes] = '/0';
printf("Received: %s/n",buf);
close(sockfd);
return 0;
}