linux socket程式設計(多執行緒)

2021-09-14 06:42:26 字數 3834 閱讀 9329

1,客戶端

#include #include #include #include #include #include #include #include #include #include /*

強調:當客戶端連線伺服器時,伺服器會產生乙個新的檔案描述符(套接字)與客戶端互動,這個新的套接字不是伺服器端的監聽套接字

套接字是全雙工的,在乙個網路管道中的兩端,每端都可以進行讀寫操作。

*/typedef struct _recvmodel

recvmodel;

//send message

void * send_thread(void *arg)

int st = *(int *) arg;

char buf[1024] = ;

while (1)

memset(buf, 0, sizeof(buf));

}return null;

}//recv message

void * recv_thread(void * arg)

recvmodel * model = (recvmodel *) arg;

int flag = 0;

char buf[1024] = ;

while (1)

else if (flag == -1)

printf("from %s:%s", inet_ntoa(model->addr->sin_addr), buf);

memset(buf, 0, sizeof(buf));

}return null;

}int main(int arg, char *args)

//定義ip位址結構

struct sockaddr_in addr;

memset(&addr, 0, sizeof(addr));

//設定tcp/ip連線

addr.sin_family=af_inet;

//設定埠號

addr.sin_port = htons(8080);

//設定允許連線位址

addr.sin_addr.s_addr = inet_addr("192.168.1.102");

//connect server

int numx = connect(st, (struct sockaddr *) &addr, sizeof(addr));

if (numx == -1)

recvmodel model;

model.st = st;

model.addr = &addr;

//開啟多執行緒--執行緒1接收訊息,執行緒2傳送訊息

pthread_t thr1, thr2;

if (pthread_create(&thr1, null, send_thread, &st) != 0)

if (pthread_create(&thr2, null, recv_thread, &model) != 0)

pthread_join(thr1, null);

pthread_join(thr2, null);

end: close(st);

return 0;

}

2,伺服器端

//網路程式設計--服務端

#include #include #include #include #include #include #include #include #include #include typedef struct _recvmodel

recvmodel;

//send message

void * send_thread(void *arg)

int st = *(int *) arg;

char buf[1024] = ;

while (1)

memset(buf, 0, sizeof(buf));

}return null;

}//recv message

void * recv_thread(void * arg)

recvmodel * model = (recvmodel *) arg;

int flag = 0;

char buf[1024] = ;

while (1)

else if (flag == -1)

printf("from %s:%s", inet_ntoa(model->addr->sin_addr), buf);

memset(buf, 0, sizeof(buf));

}return null;

}int main(int arg, char *args)

//設定系統位址可重用

int on = 1;

if (setsockopt(st, sol_socket, so_reuseaddr, &on, sizeof(on)) == -1)

//定義ip位址結構

struct sockaddr_in addr;

memset(&addr, 0, sizeof(addr));

//設定tcp/ip連線

addr.sin_family = af_inet;

//設定埠號

addr.sin_port = htons(8080);

//設定允許連線位址

addr.sin_addr.s_addr = htonl(inaddr_any);

//bind ip

if (bind(st, (struct sockaddr *) &addr, sizeof(addr)) == -1)

//監聽連線

if (listen(st, 20) == -1)

//接收客戶端連線(阻塞)

struct sockaddr_in client_addr;

memset(&client_addr, 0, sizeof(client_addr));

socklen_t client_addrlen = sizeof(client_addr);

//強調:這裡的client_addrlen並不是為了在函式中設定client_addrlen的值,而是通過client_addrlen的值來判斷client_addr是乙個什麼型別的結構,

//所以這裡client_addrlen設定為0是錯誤的,必須是struct sockaddr_in這個結構的大小,不然返回值不正確

int client_st = accept(st, (struct sockaddr *) &client_addr,

&client_addrlen);

if (client_st == -1)

recvmodel model;

model.st = client_st;

model.addr = &client_addr;

printf("accept by=%s\n",inet_ntoa(client_addr.sin_addr));

//開啟多執行緒--執行緒1接收訊息,執行緒2傳送訊息

pthread_t thr1, thr2;

if (pthread_create(&thr1, null, send_thread, &client_st) != 0)

if (pthread_create(&thr2, null, recv_thread, &model) != 0)

pthread_join(thr1, null);

pthread_join(thr2, null);

//關閉客戶端管道

close(client_st);

end: close(st);

return 0;

}

linux socket 程式設計

兩段程式 可用於開發板和主機之間的資料傳輸,很管用!file client.c檔案傳輸客戶端程式示例 本檔案是客戶機的 include for sockaddr in include for socket include for socket include for printf include f...

linux socket程式設計

雙休日無聊透頂,看了四五集 反恐24小時 實在不想看了,於是就想搞linux的socket programming來玩玩,前期資料都準備好 早就想寫個看看了。首先,寫個簡單的client端的程式 呵,其實是copy!server端用的是網上當的乙個除錯工具 一開始用的是以前用過的乙個多執行緒執行的介...

Linux Socket程式設計 執行緒

本章主要列舉伺服器程式的各種網路模型,示例程式以及效能對比後面再寫。一 分類依據。伺服器的網路模型分類主要依據以下幾點 1 是否阻塞方式處理請求,是否多路復用,使用哪種多路復用函式 2 是否多執行緒,多執行緒間如何組織 3 是否多程序,多程序的切入點一般都是accept函式前 二 分類。首先根據是否...