C語言實(shí)現(xiàn)socket簡單通信實(shí)例
本文實(shí)例講述了C語言實(shí)現(xiàn)socket簡單通信的方法,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
服務(wù)器端代碼如下:
/* ============================================================================ Name : server.c Author : king Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdlib.h> #include <pthread.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> /* inet(3) functions */ #include <stdlib.h> #include <errno.h> #include <stdio.h> #include <string.h> int handle(int point); int main(void) { int sfd, ind; struct sockaddr_in addr; struct sockaddr_in clent; char resv[1024], sendbuf[1024]; char buf[1024]; char * myaddr = "192.168.231.128"; int ret; // 返回值設(shè)置 socklen_t lent; int pid; addr.sin_family = AF_INET; //IPv4 Internet protocols addr.sin_port = htons(5050); //這里輸入服務(wù)器端口號 addr.sin_addr.s_addr = inet_addr(myaddr); ; //INADDR_ANY表示本機(jī)IP //獲取socket描述符,IPV4asd printf("socket start \n"); sfd = socket(AF_INET, SOCK_STREAM, 0); if (sfd < 0) { printf("socket error \n"); return -1; } printf("bind start \n"); //將套接子與指定端口鏈接 if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) { printf("bind error \n"); return -1; } //監(jiān)聽套接子 printf("listen start \n"); if (listen(sfd, 1024) < 0) { printf("listen error \n"); return -1; } for (;;) { //接受來自客戶端的信息 printf("accept start \n"); memset(&clent, 0, sizeof(clent)); lent = sizeof(clent); ind = accept(sfd, (struct sockaddr *) &clent, &lent); if (ind < 0) { printf("accept error %d \n", ind); return -1; } printf("infor \n"); printf("clent addr%s porit %d\n", inet_ntop(AF_INET, &clent.sin_addr, buf, sizeof(buf)), ntohs(clent.sin_port)); pid = fork(); if (pid == 0) { //子進(jìn)程 close(sfd); handle(ind); } else if (pid < 0) { //error close(ind); } else { //父進(jìn)程 } } return EXIT_SUCCESS; } int handle(int point) { int retn; char buf[1024]; for (;;) { retn = read(point, buf, sizeof(buf)); if (retn < 0) { printf("read error \n"); close(point); break; } else if (retn == 0) { printf("client exit \n"); close(point); break; } printf("client:%s\n", buf); if (strcmp("exit", buf) == 0) { printf("exit \n"); close(point); return 0; } } return 0; }
客戶端代碼如下:
/* ============================================================================ Name : client.c Author : king Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> /* inet(3) functions */ int handle(int fd); int main(void) { int nsd; char buf[1024]; char * myaddr = "192.168.231.128"; struct sockaddr_in addr; printf("welcome to echo client\n"); nsd = socket(AF_INET, SOCK_STREAM, 0); printf("connect start1 \n"); //bzero(addr, sizeof(*addr)); memset(&addr,0,sizeof(addr)); printf("connect start2 \n"); addr.sin_family = AF_INET; addr.sin_port = htons(5050); addr.sin_addr.s_addr=inet_addr(myaddr); printf("connect start3 \n"); if (connect(nsd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0) { printf("connect error \n"); return -1; } sleep(5); printf("handle start\n"); handle(nsd); close(nsd); return EXIT_SUCCESS; } int handle(int fd) { char sendl[1024], rev[1024]; int retn; for (;;) { memset(sendl,0,sizeof(sendl)); memset(rev,0,sizeof(rev)); if (fgets(sendl, 1024, stdin) == NULL) { break; } // printf("wirte start\n"); write(fd, sendl, strlen(sendl)); read(fd, rev,strlen(rev)); } return 0; }
注意:
int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
記住一定是值 addrlen
accept socklen_t *addrlen要是一個(gè)指針
希望本文所述對大家C語言網(wǎng)絡(luò)程序設(shè)計(jì)的學(xué)習(xí)有所幫助。
上一篇:C++實(shí)現(xiàn)旋轉(zhuǎn)數(shù)組的二分查找
欄 目:C語言
本文標(biāo)題:C語言實(shí)現(xiàn)socket簡單通信實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3336.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)