(1)socket通信的TCP/IP过程:
(2)socket通信的UDP过程:
(3)客户端服务器的框架设计:
(4)socket通信的TCP/IP的代码过程:
(二)客户端,服务器的多进程范例:
(1)服务器端:
#include <sys/types.h>
#include <sys/socket.h> // 包含套接字函数库
#include <stdio.h>
#include <netinet/in.h> // 包含AF_INET相关结构
#include <arpa/inet.h> // 包含AF_INET相关操作的函数
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/shm.h>
#define MYPORT 8887
#define MYKEY 12345
#define SIZE 10240
int main()
{
char buf[100];
memset(buf,0,100);
int server_sockfd,client_sockfd;
socklen_t server_len,client_len;
struct sockaddr_in server_sockaddr,client_sockaddr;
printf("\n======================server initialization======================\n");
server_sockfd = socket(AF_INET,SOCK_STREAM, 0); // 定义套接字类型
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(MYPORT);
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
server_len = sizeof(server_sockaddr);
//允许重复使用本地地址和套接字绑定
int on = 1;
setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
//绑定端口
if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,server_len)==-1)
{
perror("bind");
exit(1);
}
//监听端口
if(listen(server_sockfd,5) == -1)
{
perror("listen");
exit(1);
}
client_len = sizeof(client_sockaddr);
pid_t ppid,pid;
while(1)
{
if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_sockaddr,&client_len))==-1)
{
perror("accept error");
exit(1);
}
else
{
send(client_sockfd,"You have connect Server!",strlen("You have connect Server!"),0);
}
printf("\n%s:%d Login server!\n\n",inet_ntoa(client_sockaddr.sin_addr), ntohs(client_sockaddr.sin_port));
ppid = fork(); //创建子进程
if(ppid == -1)
{
printf("fork 1 failed:");
}
else if(ppid == 0) //子进程用于接收客户端信息并发送
{
int recvbytes;
pid = fork(); //再次创建子进程
if(pid == -1)
{
printf("fork 2 failed:");
exit(1);
}
else if(pid == 0) //子进程的子进程用于接收消息
{
while(1)
{
bzero(buf,100);
if((recvbytes = recv(client_sockfd,buf,100,0))==-1)
{
perror("read client_sockfd failed:");
}
else if(recvbytes != 0)
{
buf[recvbytes] = '\0';
usleep(10000);
printf("%s:%d said:%s\n",inet_ntoa(client_sockaddr.sin_addr), ntohs(client_sockaddr.sin_port), buf);
//将客户端发送过来的消息发回给客户
if(send(client_sockfd,buf,recvbytes,0)==-1){
perror("send error");
break;
}
}
}
}
else if(pid>0) //此时的id为子进程id
{
}
}
else if(ppid>0)
{
//总父进程中关闭client_sockfd(因为有另一个副本在子进程中运行了)返回等待接收消息
close(client_sockfd);
}
}
return 0;
}
------------------
(2)客户端:
#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>
#include<unistd.h>
#include <arpa/inet.h>
#define SERVER_PORT 8887 /* 客户机连接远程主机的端口 */
#define MAXDATASIZE 100 /* 每次可以接收的最大字节 */
#define SERVER_IP "192.168.11.8" /* 服务器的IP地址 */
int main()
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct sockaddr_in server_addr;
printf("\n======================client initialization======================\n");
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
bzero(&(server_addr.sin_zero),sizeof(server_addr.sin_zero));
if (connect(sockfd, (struct sockaddr *)&server_addr,sizeof(struct sockaddr_in)) == -1)
{
perror("connect");
exit(1);
}
//循环输入文字
while(1)
{
bzero(buf,MAXDATASIZE);
printf("\nBegin receive...\n");
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
else if (numbytes > 0)
{
int len, bytes_sent;
buf[numbytes] = '\0';
printf("Received: %s\n",buf);
printf("Send:");
char *msg;
scanf("%s",msg);
len = strlen(msg);
//发送至服务器
if(send(sockfd,msg,len,0) == -1)
{
perror("send error");
}
}
else
{
//numbytes=0,表示socket已断开
printf("soket end!\n");
}
}
close(sockfd);
return 0;
}
---------
(3)结果分析:
Linux下进行编译:
gcc Server.c -o server
gcc Client.c -o client
分析:不足之处:一个IP地址主机只能是一个客户端。