[linux] C语言Linux系统编程-socket回声客户端

[linux] C语言Linux系统编程-socket回声客户端,第1张

概述回声客户端:1.所谓“回声”,是指客户端向服务器发送一条数据,服务器再将数据原样返回给客户端,就像声音一样,遇到障碍物会被“反d回来”。2.客户端也可以使用 write() / send() 函数向服务器发送数据,服务器也可以使用 read() / recv() 函数接收数据 #define BUF_SIZE 1001.#开头的是预处理命令2.#define是宏定义,在编译预处理时,会对宏名进行宏代换/宏展开,就是替换掉 scanf("%s", bufSend);1.scanf() 读取到空格时认为一个字符串输入结束2.可以使用gets()替换echo_server.c #include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#include <netinet/in.h>#define BUF_SIZE 100int main(){//创建套接字int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//将套接字和IP、端口绑定struct sockaddr_in serv_addr;memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充serv_addr.sin_family = AF_INET; //使用IPv4地址serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址serv_addr.sin_port = htons(1234); //端口bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));//进入监听状态,等待用户发起请求listen(serv_sock, 20);//接收客户端请求struct sockaddr_in clnt_addr;socklen_t clnt_addr_size = sizeof(clnt_addr);int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);char buffer[BUF_SIZE]; //缓冲区int strLen = read(clnt_sock, buffer, BUF_SIZE); //接收客户端发来的数据write(clnt_sock, buffer,sizeof(buffer));//关闭套接字close(clnt_sock);close(serv_sock);return 0;} echo_client.c#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#define BUF_SIZE 100int main(){//创建套接字int sock = socket(AF_INET, SOCK_STREAM, 0);//向服务器(特定的IP和端口)发起请求struct sockaddr_in serv_addr;memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充serv_addr.sin_family = AF_INET; //使用IPv4地址serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址serv_addr.sin_port = htons(1234); //端口connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));//回声客户端char bufSend[BUF_SIZE];printf("输入字符串: ");scanf("%s",bufSend);write(sock, bufSend,sizeof(bufSend));//读取服务器传回的数据char buffer[40];read(sock, buffer, sizeof(buffer)-1);printf("服务器返回: %sn", buffer);//关闭套接字close(sock);return 0;}效果:使用while(1) 让代码进入死循环,一直监听客户端的请求echo_server.cwhile(1){int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);char buffer[BUF_SIZE]; //缓冲区int strLen = read(clnt_sock, buffer, BUF_SIZE); //接收客户端发来的数据write(clnt_sock, buffer,sizeof(buffer));//关闭套接字close(clnt_sock);}echo_client.cwhile(1){//创建套接字int sock = socket(AF_INET, SOCK_STREAM, 0);//向服务器(特定的IP和端口)发起请求struct sockaddr_in serv_addr;memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充serv_addr.sin_family = AF_INET; //使用IPv4地址serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址serv_addr.sin_port = htons(1234); //端口connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));//回声客户端char bufSend[BUF_SIZE];printf("输入字符串: ");scanf("%s",bufSend);write(sock, bufSend,sizeof(bufSend));//读取服务器传回的数据char buffer[40];read(sock, buffer, sizeof(buffer)-1);printf("服务器返回: %sn", buffer);//关闭套接字close(sock);} 效果:

回声客户端:

1.

2.

#define BUF_SIZE 100

1.#

2.#define

scanf("%s",bufSend);

1.scanf()

2.

<div >

#include <.h>.h> BUF_SIZE 100         serv_sock =        &serv_addr,,(serv_addr));          serv_addr.sin_family = AF_INET;          serv_addr.sin_addr.s_addr = inet_addr();          serv_addr.sin_port = htons();          bind(serv_sock,( sockaddr*)&serv_addr,        Listen(serv_sock,        =  clnt_sock = accept(serv_sock,( sockaddr*)&clnt_addr,&    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;char</span> buffer[BUF_SIZE];  <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;缓冲区</span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> strLen = read(clnt_sock,buffer,BUF_SIZE);  <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;接收客户端发来的数据</span>    write(clnt_sock,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;sizeof</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;(buffer));    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" >关闭</a><a href="https://www.jb51.cc/tag/taojie/" target="_blank" >套接</a>字</span>

<span > close(clnt_sock);
close(serv_sock);
<span >return <span >0<span >;
}

echo_clIEnt.c

#include <.h> BUF_SIZE 100 sock = socket(AF_INET, &serv_addr,(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(); serv_addr.sin_port = htons(); connect(sock, </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;回声客户端</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;char</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; bufSend[BUF_SIZE]; printf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;输入字符串: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); scanf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;<a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;,bufSend); write(sock,bufSend,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;sizeof</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;(bufSend)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;读取<a href="https://m.jb51.cc/tag/fuwuqi/" target="_blank" >服务器</a>传回的数据</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;char</span> buffer[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;40</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;]; read(sock,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;sizeof</span>(buffer)-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); printf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;<a href="https://m.jb51.cc/tag/fuwuqi/" target="_blank" >服务器</a>返回: <a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a>\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;,buffer); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" >关闭</a><a href="https://www.jb51.cc/tag/taojie/" target="_blank" >套接</a>字</span>

<span > close(sock);
<span >return <span >0<span >;
}

效果:

使用while(1) 让代码进入死循环,一直监听客户端的请求

echo_server.c

( clnt_sock = accept(serv_sock,& </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;char</span> buffer[BUF_SIZE]; <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;缓冲区</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> strLen = read(clnt_sock,BUF_SIZE); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;接收客户端发来的数</span>

<span >据
write(clnt_sock,<span >sizeof<span >(buffer));
<span >//<span >关闭套接字
<span > close(clnt_sock);
}

echo_clIEnt.c

( sock = socket(AF_INET, &serv_addr,(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(); serv_addr.sin_port = htons(); connect(sock, </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;回声客户端</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;char</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; bufSend[BUF_SIZE]; printf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;输入字符串: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); scanf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;<a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;,bufSend); write(sock,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;sizeof</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;(bufSend)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;读取<a href="https://m.jb51.cc/tag/fuwuqi/" target="_blank" >服务器</a>传回的数据</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;char</span> buffer[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;40</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;]; read(sock,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;sizeof</span>(buffer)-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); printf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;<a href="https://m.jb51.cc/tag/fuwuqi/" target="_blank" >服务器</a>返回: <a href="https://www.jb51.cc/tag/s/" target="_blank" >%s</a>\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;,buffer); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" >关闭</a><a href="https://www.jb51.cc/tag/taojie/" target="_blank" >套接</a>字</span> close(sock);}</pre>

 效果:

总结

以上是内存溢出为你收集整理的[linux] C语言Linux系统编程-socket回声客户端全部内容,希望文章能够帮你解决[linux] C语言Linux系统编程-socket回声客户端所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1051134.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存