回声客户端:
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">char</span> buffer[BUF_SIZE]; <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">缓冲区</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> strLen = read(clnt_sock,buffer,BUF_SIZE); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">接收客户端发来的数据</span> write(clnt_sock,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">sizeof</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">(buffer)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"><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 </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">回声客户端</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">char</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> bufSend[BUF_SIZE]; printf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">输入字符串: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); scanf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"><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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">,bufSend); write(sock,bufSend,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">sizeof</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">(bufSend)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">读取<a href="https://m.jb51.cc/tag/fuwuqi/" target="_blank" >服务器</a>传回的数据</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">char</span> buffer[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">40</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">]; read(sock,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">sizeof</span>(buffer)-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); printf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"><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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">,buffer); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"><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);
}
效果:
使用while(1) 让代码进入死循环,一直监听客户端的请求
echo_server.c
( clnt_sock = accept(serv_sock,& </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">char</span> buffer[BUF_SIZE]; <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">缓冲区</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> strLen = read(clnt_sock,BUF_SIZE); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">接收客户端发来的数</span>
<span >据
write(clnt_sock,
<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">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">回声客户端</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">char</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> bufSend[BUF_SIZE]; printf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">输入字符串: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); scanf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"><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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">,bufSend); write(sock,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">sizeof</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">(bufSend)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">读取<a href="https://m.jb51.cc/tag/fuwuqi/" target="_blank" >服务器</a>传回的数据</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">char</span> buffer[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">40</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">]; read(sock,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">sizeof</span>(buffer)-<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); printf(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"><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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">,buffer); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"><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回声客户端所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)