先总结下socket创建流程:
服务器端调用accept函数后,程序就会等待客户端调用connect函数发出连接请求,接收到客户请求,于是双方就建立了连接。之后,服务器端和客户端就可以利用read和write函数进行通信了。
main()
{
int Ser_socket = socket(AF_INET, SOCK_STREAM, 0); //创建一个负责监听的套接字
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); //initialize addr
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(9990); /* 端口号,随意 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址 */
int resp = bind(Ser_socket , (struct sockaddr *)&addr, sizeof(addr)); //连接
int lresp = listen(Ser_socket , 20);
while(1){
struct sockaddr_in cliaddr;
int client_socket = accept(Ser_socket , (struct sockaddr *)&cliaddr , sizeof(cliaddr));
char cmd[1024];
int ret = read(client_socket, cmd, 1023);
cmd[ret]='\0';
//处理指令;
if(strcmp(cmd,"quit") == 0){
break;
}
}
close(Ser_socket );
}
main()
{
int Ser_socket = socket(AF_INET, SOCK_STREAM, 0); //创建一个负责监听的套接字
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); //initialize addr
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(9990); /* 端口号,随意 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址 */
int resp = bind(Ser_socket , (struct sockaddr *)&addr, sizeof(addr)); //连接
int lresp = listen(Ser_socket , 20);
while(1){
struct sockaddr_in cliaddr;
int client_socket = accept(Ser_socket , (struct sockaddr *)&cliaddr , sizeof(cliaddr));
int pid = fork();
if(pid == -1){
break;
}
if(pid > 0){
//父进程一开始就要把父进程的接收函数关闭掉,防止父进程在接收函数处阻塞,导致子进程不能创建成功.
close(client_socket);
continue;
}
if(pid == 0){
//子进程在一创建就要把监听函数关闭,不然会导致服务器功能的紊乱。
close(Ser_socket);
char cmd[1024];
int ret = read(client_socket, cmd, 1023);
cmd[ret]='\0';
//子进程收到客户端指令。
//xxxxxxxx处理指令
break;
}
close(Ser_socket );
}
main()
{
int Ser_socket = socket(AF_INET, SOCK_STREAM, 0); //创建一个负责监听的套接字
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); //initialize addr
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(9990); /* 端口号,随意 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址 */
int resp = bind(Ser_socket , (struct sockaddr *)&addr, sizeof(addr)); //连接
int lresp = listen(Ser_socket , 20);
while(1){
struct sockaddr_in cliaddr;
int client_socket = accept(Ser_socket , (struct sockaddr *)&cliaddr , sizeof(cliaddr));
pthread_t id;
//clientMethod为此线程客户端,要执行的程序。
pthread_create(&id, NULL, (void *)clientMethod, (void *)client_socket);
//要将id分配出去。
pthread_detach(id);
}
close(Ser_socket );
}
main()
{
int Cli_socket = socket(AF_INET, SOCK_STREAM, 0); //创建和服务器连接套接字
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));//initialize clear 0
addr.sin_family = AF_INET; /* Internet地址族 */
addr.sin_port = htons(9990); /* 端口号 */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* IP地址 */
inet_aton("127.0.0.1", &(addr.sin_addr));
int addrlen = sizeof(addr);
int Conn_socket = connect(Cli_socket , (struct sockaddr *)&addr, addrlen); //连接服务器
char cmd[1024];
int end = 0;
while(1) //向服务器发送数据
{
if(end == 1) break;
memset(cmd, 0, sizeof(cmd));
printf("please input command:\n");
gets(cmd);
if(strcmp(cmd,"quit") == 0 ){
end = 1;
}else{
write(Cli_socket ,cmd,strlen(cmd));
}
}
close(Conn_socket );
}
仅为个人理解,如有不足,请指教。 https://blog.csdn.net/weixin_35811044