基於TCP 實現客戶端之間通訊 1

2021-08-14 07:48:54 字數 3148 閱讀 7470

前段時間學習了基於tcp協議下實現伺服器與乙個客戶端的通訊,伺服器與多個客戶端之間的通訊,

以及客戶端之間的互相通訊。

下面就是我寫的利用tcp和多執行緒技術實現客戶端之間互相通訊的**:

伺服器端:tcpserverb.c

#include #include #include #include #include #include #include #include #include #define port   8888

struct info

;void *myreceive(void *arg)

; int ret;

pthread_detach(pthread_self());

while (1)

if (!strcmp(recvbuf.buf,"bye"))

// printf("recv from client : %s\n", buf);

ret = send(recvbuf.tofd, &recvbuf, sizeof(recvbuf),0);

if (-1 == ret)

memset(&recvbuf, 0, sizeof(recvbuf)); }}

int main()

,i = 0;

char buf[100] = ;

struct sockaddr_in server_addr; //用於存放伺服器本身的資訊,包括自己的port和ip

struct sockaddr_in client_addr; //接受客戶端連線的時候, 用於存放客戶端資訊

pthread_t tid[100] = ;

printf("start server!\n");

sockfd = socket(pf_inet, sock_stream, 0); //建立socket,處理客戶端的連線,不用於傳送資訊

if (-1 == sockfd)

int opt = 1;

setsockopt(sockfd, sol_socket, so_reuseaddr, &opt, sizeof(opt));

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

server_addr.sin_family = pf_inet; //位址族

server_addr.sin_port = port; //指定socket的埠號

server_addr.sin_addr.s_addr = inet_addr("192.168.0.128"); //本機ip

ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); //上述資訊繫結到socket

if (-1 == ret)

printf("listening...\n");

ret = listen(sockfd, 5); //監聽,是否有客戶端發起連線

if(-1 == ret)

while(1)

printf("accept %d , port %d\n", fd[i], client_addr.sin_port);

ret = pthread_create(&tid[i], null, myreceive, (void *)&fd[i]);

if (0 != ret)

i++;

} close(sockfd);

return 0;

}

客戶端:tcpclientb.c

#include #include #include #include #include #include #include #include #define port    8888

struct info

;pthread_t tid[2] = ;

void *send(void *arg)

; int ret;

while (1)

if (!strcmp(sendbuf.buf, "bye"))

memset(&sendbuf, 0, sizeof(sendbuf)); }}

void *recv(void *arg)

; int ret,oldtype;

pthread_setcanceltype(pthread_cancel_asynchronous, &oldtype);

while(1)

printf("\t\t\t%s\n", recvbuf.buf);

memset(&recvbuf, 0, sizeof(recvbuf)); }}

int main()

memset(&server_addr, 0, sizeof(server_addr)); //都是伺服器的訊息

server_addr.sin_family = pf_inet;

server_addr.sin_port = port;

server_addr.sin_addr.s_addr = inet_addr("192.168.0.128");

printf("start connecting...\n");

ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); //發起連線

if (-1 == ret)

ret = pthread_create(&tid[0], null, send, (void *)&sockfd);

if (0 != ret)

ret = pthread_create(&tid[1], null, recv, (void *)&sockfd);

if (0 != ret)

pthread_join(tid[0], null);

pthread_join(tid[1], null);

close(sockfd);

return 0;

}

TCP通訊(客戶端)

修改自網路 include include include pragma comment lib,ws2 32.lib int client void unsigned short port int main int argc,char argv int client void if isockcl...

TCP通訊,多客戶端通訊(客戶端 服務端)

客戶端和伺服器間的交流,客戶端傳送資訊,伺服器接收到,並返回資訊 長連線建立socket連線服務端 指定ip位址,埠號 通過ip位址找對應的伺服器 呼叫socket的getinputstream 和getoutputstream 方法獲取和服務端相連的io流 輸入流可以讀取服務端輸出流寫出的資料 輸...

TCP連線實現 客戶端 服務端 通訊

客戶端 client.c include include include include include include include include define max buf 512 int main int argc,char argv 設定伺服器位址結構體 bzero server ad...