一、多进程服务器
1、服务器端代码:
#include
#include
#include
#include
#include
#include
#include
//./tcp_server 127.0.0.1 8080
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");
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");
exit(3);
}
if (listen(sock, 10) < 0)
{
perror("listen");
exit(4);
}
return sock;
}
// ./ tcp_server ip port
int main(int argc, char *argv[])
{
if (argc != 3)
{
usage(argv[0]);
return 1;
}
//
int listen_sock = startup(argv[1], atoi(argv[2])); //ip,.
while (1)
{
struct sockaddr_in client;
socklen_t len = sizeof(client);
int new_sock = accept(listen_sock, \
(struct sockaddr*)&client, &len);
if (new_sock < 0)
{
perror("accept");
continue;
}
printf("get a new client, %s: %d\n",inet_ntoa(client.sin_addr), ntohs(client.sin_port));
//version 2 mut process
//
pid_t id = fork();
if (id < 0)
{
close(new_sock);
}else if (id == 0){//child
close(listen_sock);
if (fork() > 0){
exit(0);
}
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 quit...\n");
break;
}else{
perror("read");
close(new_sock);
break;
}
}
close(new_sock);
}else{//father
close(new_sock);
}
//version 1
// 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 quit...\n");
// break;
// }else{
// perror("read");
// close(new_sock);
// break;
// }
// }
}
}
2、客户端代码:
#include
#include
#include
#include
#include
#include
#include
static void usage(const char *proc)
{
printf("Usage: %s [server_ip] [server_port]\n", proc);
}
// ./ tcp_client server_ip server_port
int main(int argc, char *argv[])
{
if (argc != 3)
{
usage(argv[0]);
return 1;
}
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror("socket");
return 1;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
{
perror("connect");
return 2;
}
char buf[1024];
while(1)
{
printf("Please Enter# ");
fflush(stdout);
ssize_t s = read(0, buf, sizeof(buf)-1);
if (s > 0)
{
buf[s-1] = 0;
write(sock, buf, strlen(buf));
s = read(sock, buf, sizeof(buf)-1);
if (s > 0)
{
buf[s] = 0;
printf("server echo# %s\n", buf);
}
}
}
}
服务器端运行指令:
./tcp_server 192.168.1.106 8080
运行结果:
二、多线程服务器
tcp_server服务器端的代码:
#include
#include
#include
#include
#include
#include
#include
#include
//./tcp_server 127.0.0.1 8080
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");
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");
exit(3);
}
if (listen(sock, 10) < 0)
{
perror("listen");
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 quit...\n");
break;
}else{
perror("read");
close(new_sock);
break;
}
}
}
// ./ tcp_server ip port
int main(int argc, char *argv[])
{
if (argc != 3)
{
usage(argv[0]);
return 1;
}
//
int listen_sock = startup(argv[1], atoi(argv[2])); //ip,.
while (1)
{
struct sockaddr_in client;
socklen_t len = sizeof(client);
int new_sock = accept(listen_sock, \
(struct sockaddr*)&client, &len);
if (new_sock < 0)
{
perror("accept");
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);
//version 2 mut process
//
// pid_t id = fork();
// if (id < 0)
// {
// close(new_sock);
// }else if (id == 0){//child
//
// close(listen_sock);
//
// if (fork() > 0){
// exit(0);
// }
// 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 quit...\n");
// break;
// }else{
// perror("read");
//
// close(new_sock);
// break;
// }
// }
// close(new_sock);
// }else{//father
// close(new_sock);
// }
//version 1
// 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 quit...\n");
// break;
// }else{
// perror("read");
// close(new_sock);
// break;
// }
// }
}
}