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


#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; }

 

参考:

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

  

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值