導航:首頁 > 軟體知識 > c語言怎麼寫通信程序

c語言怎麼寫通信程序

發布時間:2023-10-22 00:53:15

㈠ 如何用C語言編寫一個簡單的聊天室程序

這樣:

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#include <netdb.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <sys/types.h>

#include <arpa/inet.h>

#include <pthread.h>

#define MAXLINE 100;

void *threadsend(void *vargp);

void *threadrecv(void *vargp);

int main()

{

int *clientfdp;

clientfdp = (int *)malloc(sizeof(int));

*clientfdp = socket(AF_INET,SOCK_STREAM,0);

struct sockaddr_in serveraddr;

struct hostent *hp;

bzero((char *)&serveraddr,sizeof(serveraddr));

serveraddr.sin_family = AF_INET;

serveraddr.sin_port = htons(15636);

serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){

printf("connect error ");

exit(1);

}

pthread_t tid1,tid2;

printf("connected ");

while(1){

pthread_create(&tid1,NULL,threadsend,clientfdp);

pthread_create(&tid2,NULL,threadrecv,clientfdp);

}

return EXIT_SUCCESS;

}

void *threadsend(void * vargp)

{

//pthread_t tid2;

int connfd = *((int *)vargp);

int idata;

char temp[100];

while(1){

//printf("me: ");

fgets(temp,100,stdin);

send(connfd,temp,100,0);

printf(" client send OK ");

}

printf("client send ");

return NULL;

}

void *threadrecv(void *vargp)

{

char temp[100];

int connfd = *((int *)vargp);

while(1){

int idata = 0;

idata = recv(connfd,temp,100,0);

if(idata > 0){

printf("server : %s ",temp);

}

}

return NULL;

}

(1)c語言怎麼寫通信程序擴展閱讀:

注意事項

linux下編譯多線程代碼時,shell提示找不到 pthread_create函數,原因是 pthread.h不是linux系統默認載入的庫文件,應該使用類似如下gcc命令進行編譯:

gcc echoserver.c -lpthread -o echoserver

只要注意 -lpthread參數就可以了。

閱讀全文

與c語言怎麼寫通信程序相關的資料

熱點內容
什麼是信息系統動畫 瀏覽:936
法院申請再審申請都是什麼程序 瀏覽:33
如何做接種數據 瀏覽:605
java怎麼連接遠程資料庫 瀏覽:505
如何統計加粉數據 瀏覽:138
技術基地如何接地氣 瀏覽:50
南山南哪裡有正規古玩交易市場 瀏覽:484
手機騰訊文檔怎麼篩選數據 瀏覽:980
東時轉債超20停牌怎麼交易 瀏覽:252
益陽市酒店翻新怎麼代理 瀏覽:526
扇形表數據變了怎麼改變扇形面積 瀏覽:176
財稅代理公司如何辦理 瀏覽:539
機票代理怎麼發簡訊 瀏覽:165
國慶期間房價數據如何 瀏覽:779
昆明二手貨車交易市場在哪裡 瀏覽:103
貿易出行機票代理怎麼聯系 瀏覽:496
台江哪裡玉器現場交易 瀏覽:35
如何用手機拍攝產品包包 瀏覽:723
jk出稿需要哪些數據 瀏覽:765
租金的交易方式有哪些 瀏覽:876