linux socket编程.doc
最近看了一些网络编程的书籍,一直以来总感觉网络编程神秘莫测,其实网络编程入门还是很容易学的,下面这些代码是我在linux下编写的,已经运行过了,编译之后就可以运行了。有不足之处希望大家多多指出,共同学习交流。TCP协议:服务器端:tcp_server.c#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int main(int argc, char *argv)int s;int fd;int len;struct sockaddr_in my_addr; /服务器网络地址结构体struct sockaddr_in remote_addr; /客户端网络地址结构体int sin_size;char bufBUFSIZ; /数据传送的缓冲区memset(&my_addr,0,sizeof(my_addr); /数据初始化-清零my_addr.sin_family=AF_INET; /设置为IP通信my_addr.sin_addr.s_addr=INADDR_ANY;/服务器IP地址-允许连接到所有本地地址上my_addr.sin_port=htons(8000); /服务器端口号/*创建服务器端套接字-IPv4协议,面向连接通信,TCP协议*/if(s=socket(PF_INET,SOCK_STREAM,0)<0)perror("socket");return 1;/*将套接字绑定到服务器的网络地址上*/if (bind(s,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)<0)perror("bind");return 1;/*监听连接请求-监听队列长度为5*/listen(s,5);sin_size=sizeof(struct sockaddr_in);/*等待客户端连接请求到达*/if(fd=accept(s,(struct sockaddr *)&remote_addr,&sin_size)<0)perror("accept");return 1;printf("accept client %sn",inet_ntoa(remote_addr.sin_addr);len=send(fd,"Welcome to my servern",21,0);/发送欢迎信息/*接收客户端的数据并将其发送给客户端-recv返回接收到的字节数,sand返回发送的字节数*/while(len=recv(fd,buf,BUFSIZ,0)>0)buflen='0'printf("%sn",buf);if(send(fd,buf,len,0)<0)perror("write");return 1;close(fd);close(s);return 0;客户端:tcp_client.c#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int main(int argc, char *argv)int s;int len;struct sockaddr_in remote_addr; /客户端网络地址结构体char bufBUFSIZ; /数据传送的缓冲区memset(&remote_addr,0,sizeof(remote_addr); /数据初始化-清零remote_addr.sin_family=AF_INET; /设置为IP通信remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");/服务器IP地址remote_addr.sin_port=htons(8000); /服务器端口号/*创建客户端套接字-IPv4协议,面向连接通信,TCP协议*/if(s=socket(PF_INET,SOCK_STREAM,0)<0)perror("socket");return 1;/*将套接字绑定到服务器的网络地址上*/if (connect(s,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr)<0)perror("connect");return 1;printf("connected to servern");len=recv(s,buf,BUFSIZ,0);/接收服务器端信息buflen='0'printf("%s",buf); /打印服务器端信息/*循环的发送接收信息并打印接收信息-recv返回接收到的字节数,sand返回发送的字节数*/while(1)printf("Enter string to send:");scanf("%s",buf);if(!strcmp(buf,"quit")break;len=send(s,buf,strlen(buf),0);len=recv(s,buf,BUFSIZ,0);buflen='0'printf("received:%sn",buf);close(s);/关闭套接字return 0;UDP协议:服务器端:udp_server.c#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int main(int argc, char *argv)int s;int fd;int len;struct sockaddr_in my_addr; /服务器网络地址结构体struct sockaddr_in remote_addr; /客户端网络地址结构体int sin_size;char bufBUFSIZ; /数据传送的缓冲区memset(&my_addr,0,sizeof(my_addr); /数据初始化-清零my_addr.sin_family=AF_INET; /设置为IP通信my_addr.sin_addr.s_addr=INADDR_ANY;/服务器IP地址-允许连接到所有本地地址上my_addr.sin_port=htons(8000); /服务器端口号/*创建服务器端套接字-IPv4协议,面向无连接通信,UDP协议*/if(s=socket(PF_INET,SOCK_DGRAM,0)<0)perror("socket");return 1;/*将套接字绑定到服务器的网络地址上*/if (bind(s,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)<0)perror("bind");return 1;sin_size=sizeof(struct sockaddr_in);printf("waiting for a packet.n");/*接收客户端的数据并将其发送给客户端-recvfrom是无连接的*/if(len=recvfrom(s,buf,BUFSIZ,0,(struct sockaddr *)&remote_addr,&sin_size)<0)perror("recvfrom");return 1;printf("received packet from %s:n",inet_ntoa(remote_addr.sin_addr);buflen='0'printf("contents: %sn",buf);close(s);return 0;客户端:udp_client.c#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int main(int argc, char *argv)int s;int len;struct sockaddr_in remote_addr; /服务器端网络地址结构体int sin_size;char bufBUFSIZ; /数据传送的缓冲区memset(&remote_addr,0,sizeof(remote_addr); /数据初始化-清零remote_addr.sin_family=AF_INET; /设置为IP通信remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");/服务器IP地址remote_addr.sin_port=htons(8000); /服务器端口号/*创建客户端套接字-IPv4协议,面向无连接通信,UDP协议*/if(s=socket(PF_INET,SOCK_DGRAM,0)<0)perror("socket");return 1;strcpy(buf,"This is a test message");printf("sending: '%s'n",buf);sin_size=sizeof(struct sockaddr_in);/*向服务器发送数据包*/if(len=sendto(s,buf,strlen(buf),0,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr)<0)perror("recvfrom");return 1;close(s);return 0;socket函数API.cpphtons();/将short类型的值从主机字节序转换为网络字节序inet_addr();/将IP地址字符串转换为long类型的网络字节序gethostbyname();/获得与该域名对应的IP地址inet_ntoa();/将long类型的网络字节序转换成IP地址字符串