基於TCP協議 實現簡單的多執行緒多程序伺服器

2021-08-20 01:16:39 字數 3608 閱讀 9431

相比於udp來說,由於udp是不需要連線的,可直接通訊。所以基於udp協議的伺服器,並不需要考慮到多客戶端同時訪問伺服器時接受資料的問題。而tcp協議是需要連線的,在乙個客戶端連線上伺服器後,這個時候伺服器就開始於客戶端進行訊息的傳送與接收。那麼其它的客戶端在這個時候是無法與伺服器連線的。這個時候就需要考慮多執行緒或者多程序的伺服器來應對這個問題了。

#include

#include

#include

#include

#include

#include

#include

#include

int startup(char* ip, char* port)

int sock = socket(af_inet, sock_stream, 0);

if(sock < 0)

struct sockaddr_in local;

bzero(&local, sizeof(local));

local.sin_family = af_inet;

local.sin_addr.s_addr = inet_addr(ip);

local.sin_port = htons(atoi(port));

if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0)

if(listen(sock, 5) < 0)

return sock;

}int main(int argc, char* argv)

int listen_sock = startup(argv[1], argv[2]);

char buf[64];

struct sockaddr_in client;

socklen_t len = sizeof(client);

while(1)

while(1)

}} else

} else

} return

0;}

#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char* argv)

int sock = socket(af_inet, sock_stream, 0);

if(sock < 0)

struct sockaddr_in server;

bzero(&server, sizeof(server));

socklen_t len = sizeof(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, len) < 0)

char buf[64];

while(1)

}} close(sock);

return

0;}

#include

#include

#include

#include

#include

#include

#include

#include

#include

typedef

struct argarg;

int startup(char* ip, char* port)

int sock = socket(af_inet, sock_stream, 0);

if(sock < 0)

struct sockaddr_in local;

socklen_t len = sizeof(local);

bzero(&local, len);

local.sin_addr.s_addr = inet_addr(ip);

local.sin_port = htons(atoi(port));

local.sin_family = af_inet;

if(bind(sock, (struct sockaddr*)&local, len) < 0)

if(listen(sock, 5) < 0)

return sock;

}void service(char* ip, int port, int sock)

else

if(s == 0) else

}}void* server_client(void* ptr)

int main(int argc, char* argv)

int listen_sock = startup(argv[1], argv[2]);

struct sockaddr_in client;

socklen_t len = sizeof(client);

while(1)

arg* arg = (arg*)malloc(sizeof(arg));

arg->addr = client;

arg->sock = sock;

pthread_t tid;

pthread_create(&tid, null, server_client, (void*)arg);

pthread_detach(tid);

} return

0;}

#include

#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char* argv)

int sock = socket(af_inet, sock_stream, 0);

if(sock < 0)

struct sockaddr_in server;

bzero(&server, sizeof(server));

socklen_t len = sizeof(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, len) < 0)

char buf[64];

while(1)

}} close(sock);

return

0;}

基於TCP的Socket通訊 多執行緒

中引用的頭函式 include include include pragma comment lib,ws2 32.lib 伺服器端 1.初始化socket環境,建立套接字 wsadata wsadata word wversionrequested makeword 2,2 將兩個byte型合成乙...

基於Linux多執行緒的併發TCP服務

經過兩天的學習,終於做出來可以向指定客戶端傳送資料的tcp服務,寫本次博文的目的有二,一是將學習成果分享給正在學習tcp併發服務的同行,二是整理一下筆記,方便日後複習。首先說一下為什麼學習tcp併發服務,6個月前去面試嵌入式軟體工程師的面試題是 用兩個半小時的時間用任意語言編寫乙個可以兩個及兩個以上...

最簡單的TCP協議實現

在客戶端方面 服務端 include include include include include include include include include include using namespace std class tcpserver 初始化 void initserver 進行繫...