博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux 套接字编程入门--Hello World
阅读量:5250 次
发布时间:2019-06-14

本文共 3387 字,大约阅读时间需要 11 分钟。

  下述代码是linux套接字编程的入门代码.分为服务端和客户端源码.

  服务端代码的主要流程是绑定ip地址和端口号建立套接字,等待客户端发起访问.接受客户端请求之后,向客户端发送字符串"hello world",关闭套接字,结束程序.

  客户端代码的主要流程是向服务端对应的套接字发起请求,读取服务端发送的数据,并且打印出来.

  代码已经详细注释,更多细节不再赘述.

server.cpp

#include
#include
#include
#include
#include
#include
#include
int main(){ //创建套接字 /* * AF_INET is an address family that is used to designate * the type of addresses that your socket can communicate * with (in this case, Internet Protocol v4 addresses). * When you create a socket, you have to specify its address family, * and then you can only use addresses of that type with the socket. * The Linux kernel, for example, supports 29 other address families * such as UNIX (AF_UNIX) sockets and IPX (AF_IPX), and also communications with IRDA * and Bluetooth * (AF_IRDA and AF_BLUETOOTH, but it is doubtful you'll use these at such a low level). * For the most part, sticking with AF_INET for socket programming over * a network is the safest option. * There is also AF_INET6 for Internet Protocol v6 addresses */ /* * SOCK_STREAM * Provides sequenced, reliable, two-way, connection- * based byte streams. An out-of-band data transmission * mechanism may be supported. */ //IPPROTO_TCP 采用TCP协议 int serv_sock =socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); //将套接字和IP 端口号进行绑定 struct sockaddr_in serv_addr; //初始化结构体serv_addr memset(&serv_addr,0,sizeof(serv_addr)); //使用ipv4地址 serv_addr.sin_family=AF_INET; //设置ip地址 serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //设置端口号 serv_addr.sin_port =htons(2896); //将套接字和结构体进行绑定 结构体中存储了套接字的协议 端口号 以及ip地址 bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); //进入监听状态,等待用户发起请求 //进入被动监听状态,套接字一直处于睡眠状态,直至客户端发起请求才会被重新唤醒 listen(serv_sock,20); //客户端请求对应的套接字结构体 struct sockaddr_in client_addr; //客户端请求套接字结构体的大小 socklen_t client_addr_size =sizeof(client_addr); //用于接受客户端的请求 int client_sock =accept(serv_sock,(struct sockaddr *)&client_addr,&client_addr_size); char str[]="hello world"; //向客户端发送数据 //向客户端套接字中写入数据 write(client_sock,str,sizeof(str)); //关闭套接字 close(client_sock); close(serv_sock); return 0;}

client.cpp

#include
#include
#include
#include
#include
#include
int main(){ /* * This constant has the value 0. * It's actually an automatic choice depending on socket type and family. * If you use it, and if the socket type is SOCK_STREAM and the family is AF_INET, * then the protocol will automatically be TCP (exactly the same as if you'd used IPPROTO_TCP). * Buf if you use IPPROTO_IP together with AF_INET and SOCK_RAW, you will have an error, * because the kernel cannot choose a protocol automatically in this case. */ int sock =socket(AF_INET,SOCK_STREAM,IPPROTO_IP); struct sockaddr_in serv_addr; memset(&serv_addr,0,sizeof(serv_addr)); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); serv_addr.sin_port= htons(2896); //通过connect函数向客户端发起请求,服务器的套接字信息存储在结构体serv_addr中 connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); char buffer[40]; //通过read从套接字中读取数据 read(sock,buffer,sizeof(buffer)-1); printf("message from server: %s\n",buffer); //关闭套接字 close(sock); return 0;}

 

参考:

  1. stack_overflow 
  2. man7
  3. c语言中文网

  

转载于:https://www.cnblogs.com/zhoudayang/p/5510234.html

你可能感兴趣的文章
九.python面向对象(双下方法内置方法)
查看>>
go:channel(未完)
查看>>
[JS]递归对象或数组
查看>>
LeetCode(17) - Letter Combinations of a Phone Number
查看>>
Linux查找命令对比(find、locate、whereis、which、type、grep)
查看>>
路由器外接硬盘做nas可行吗?
查看>>
python:从迭代器,到生成器,再到协程的示例代码
查看>>
Java多线程系列——原子类的实现(CAS算法)
查看>>
在Ubuntu下配置Apache多域名服务器
查看>>
多线程《三》进程与线程的区别
查看>>
linux sed命令
查看>>
html标签的嵌套规则
查看>>
[Source] Machine Learning Gathering/Surveys
查看>>
HTML <select> 标签
查看>>
类加载机制
查看>>
tju 1782. The jackpot
查看>>
HTML5与CSS3基础(五)
查看>>
WinDbg调试C#技巧,解决CPU过高、死锁、内存爆满
查看>>
linux脚本中有source相关命令时的注意事项
查看>>
css样式表中的样式覆盖顺序
查看>>