linux 套接字编程入门--Hello World
Posted ZHOU YANG
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux 套接字编程入门--Hello World相关的知识,希望对你有一定的参考价值。
下述代码是linux套接字编程的入门代码.分为服务端和客户端源码.
服务端代码的主要流程是绑定ip地址和端口号建立套接字,等待客户端发起访问.接受客户端请求之后,向客户端发送字符串"hello world",关闭套接字,结束程序.
客户端代码的主要流程是向服务端对应的套接字发起请求,读取服务端发送的数据,并且打印出来.
代码已经详细注释,更多细节不再赘述.
server.cpp
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<arpa/inet.h> #include<sys/socket.h> #include<netinet/in.h> #include <string.h> 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
// // Created by fit on 16-5-19. // #include<stdio.h> #include<cstring> #include<stdlib.h> #include<unistd.h> #include <arpa/inet.h> #include<sys/socket.h> 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; }
参考:
- stack_overflow
- man7
- c语言中文网
以上是关于linux 套接字编程入门--Hello World的主要内容,如果未能解决你的问题,请参考以下文章