在我的上篇博客中已经详细介绍了socket编程中所需要的函数,在这里就不过多介绍了,有需要的话可以参考上一篇博客:http://blog.csdn.net/qq_36221862/article/details/73611942
多进程,多线程socket编程与单进程不同的只是服务器端的不同,客户端是一样的。
多进程:父进程fork子进程, 子进程退出,但是子进程的子进程仍在运行,变成了孤儿进程,由1号进程回收,父进程不用等待子进程的子进程的退出,它俩是爷孙进程,不用回收子进程。
pid_t id = fork();
if(id < 0)
{
close(new_sock);
}
else if(id == 0) //子进程
{
close(listen_sock); //关闭不必要的文件描述局,不关闭的话,文教描述符表会越来越少,造成资源的浪费
if(fork() > 0)
{
exit(1);
}
else if(id == 0)
{
//服务客户端
}
else
{
close(new_sock);
}
else
{
close(new_sock);
}
多进程socket编程服务器端代码:
#include
#include
#include
#include
#include
#include
#include
#include
static void usage(const char* proc) //使用说明
{
printf("Usage:%s [local_ip] [local_port]\n", proc);
}
int startup(const char* _ip, int _port) //创建套接字
{
// int sock = socket(AF_INET, SOCK_STREAM , 0);
// if(sock < 0)
/// {
// perror("socket\n");
// exit(2);
// }
**struct sockaddr_in local;
int reuse = 0;
struct sockaddr_in cliaddr, servaddr;
int sock = socket(PF_INET, SOCK_STREAM,0);
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
perror("setsockopet error\n");
return -1;
}** //避免绑定失败,在上篇博客也已经解释过了
local.sin_family = AF_INET;
local.sin_port = htons(_port);
local.sin_addr.s_addr = inet_addr(_ip);
if(bind(sock, (struct sockaddr*)&local, sizeof(local))<0)
{
perror("bind\n");
exit(3);
}
if(listen(sock, 10) < 0)
{
perror("listen\n");
exit(4);
}
return sock;
}
//tcp_server 127.0.0.1 8080
int main(int argc, char* argv[])
{
if(argc != 3)
{
usage(argv[0]);
return 1;
}
int listen_sock = startup(argv[1], atoi(argv[2])); //监听状态
struct sockaddr_in client;
socklen_t len = sizeof(client);
struct sockaddr_in cliaddr, servaddr;
while(1)
{
int new_sock = accept(listen_sock, (struct sockaddr*)&client, &len);
if(new_sock < 0)
{
perror("accept\n");
continue; //继续监听,直到有新客户到来
}
//获取新客户
printf("get a new client,%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
//服务新客户
pid_t id = fork();
if(id < 0)
{
close(new_sock);
}
else if(id == 0) //子进程
{
close(listen_sock);
if(fork() > 0)
{
exit(1);
}
else if(id == 0)
{
while(1)
{
char buf[1024000]; //服务器端先读再写
ssize_t s = read(new_sock, buf, sizeof(buf)-1);
if(s > 0)
{
buf[s] = 0;
printf("client: %s\n", buf);
write(new_sock, buf, strlen(buf));
}
else if(s == 0)
{
close(new_sock);
printf("client is quit...\n");
break;
}
else
{
perror("read\n");
close(new_sock);
exit(5);
}
}
}
close(new_sock);
}
else
{
close(new_sock);
}
break;
}
}
多线程:不用关闭多余的文件描述符表,进程有两张文件描述符表,而线程只有一张文件描述符表,共享进程的文件描述符表,因此不用关闭多余的文件描述符表。
pthread_t id;
pthread_create(&id, NULL, handlerRequest, (void*)new_sock); //创建线程
pthread_detach(id); //线程分离
多线程socket编程源代码:
#include
#include
#include
#include
#include
#include
#include -
#include
static void usage(const char* proc) //使用说明
{
printf("Usage:%s [local_ip] [local_port]\n", proc);——
}
int startup(const char* _ip, int _port) //创建套接字
{
int sock = socket(AF_INET, SOCK_STREAM , 0);
if(sock < 0)
{
perror("socket\n");
exit(2);
}
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(_port);
local.sin_addr.s_addr = inet_addr(_ip);
if(bind(sock, (struct sockaddr*)&local, sizeof(local))<0)
{
perror("bind\n");
exit(3);
}
if(listen(sock, 10) < 0)
{
perror("listen\n");
exit(4);
}
return sock;
}
void handlerRequest(void* arg)
{
int new_sock = (int)arg;
while(1)
{
char buf[1024]; //服务器端先读再写
ssize_t s = read(new_sock, buf, sizeof(buf)-1);
if(s > 0)
{
buf[s] = 0;
printf("client: %s\n", buf);
write(new_sock, buf, strlen(buf));
}
else if(s == 0)
{
close(new_sock);
printf("client is quit...\n");
break;
}
else
{
perror("read\n");
close(new_sock);
exit(5);
}
}
}
//tcp_server 127.0.0.1 8080
int main(int argc, char* argv[])
{
if(argc != 3)
{
usage(argv[0]);
return 1;
}
int listen_sock = startup(argv[1], atoi(argv[2])); //监听状态
struct sockaddr_in client;
socklen_t len = sizeof(client);
while(1)
{
int new_sock = accept(listen_sock, (struct sockaddr*)&client, &len);
if(new_sock < 0)
{
perror("accept\n");
continue; //继续监听,直到有新客户到来
}
//获取新客户
printf("get a new client,%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
//服务新客户
pthread_t id;
pthread_create(&id, NULL, handlerRequest, (void*)new_sock);
pthread_detach(id); //线程分离
}
}