linux
中的网络编程通过
socket
接口实现。
Socket
既是一种特殊的
IO
,它也是一种文件描述符。一个完整的
Socket
都有一个相关描述
{
协议,本地地址,本地端口,远程地址,远程端口
}
;每一个
Socket
有一个本地的唯一
Socket
号,由操作系统分配。
TCP服务器端
UDP服务器端
TCP服务器端
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
int main()
{
int server_fd,client_fd;//socket出来的文件描述符
struct sockaddr_in server_addr={0};//套接字地址
struct sockaddr_in client_addr={0};
int addrlen;
pid_t pid;
int nRead;
char acBuf[1024];
server_fd=socket(PF_INET,SOCK_STREAM,0);//流式套接字
if (server_fd==-1)
{
perror("socket");
return -1;
}
//让地址可复用
int yest=1;
setsockopt(server_fd,SOL_SOCKET,SO_BROADCAST,&yest,sizeof(yest));
//绑定自身地址
memset(&server_addr,0,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;//ipv4
server_addr.sin_port=htons(8888);//字节转换函数
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
if (bind(server_fd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
perror("bind");
return -1;
}
//设置半链接数
listen(server_fd,10);
signal(SIGCHLD,SIG_IGN);//屏蔽子进程结束发出的信号
//等待客户端用户的链接请求
while(1)
{
memset(&client_addr, 0, sizeof(struct sockaddr_in) );
client_fd=accept(server_fd,(struct sockaddr *)&client_addr,&addrlen);//阻塞,等待连接
if (client_fd==-1)
{
perror("accept");
return -1;
}
pid=fork();//每连接上一个客户端开一个进程
if (pid == -1)
{
perror("fork");
return -1;
}
else if (pid ==0)
{
while (1)
{
memset(acBuf, 0, 1024);
nRead=read(client_fd, acBuf, 1024);
if (nRead == 0)
{
close(client_fd);
exit(0);
}
write(client_fd, acBuf, nRead);
}
}
close(client_fd);
}
return 0;
}
TCP客户端
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int client_fd;//socket出来的文件描述符
struct sockaddr_in addr={0};//套接字地址
char acBuf[1024]={0};
client_fd=socket(PF_INET,SOCK_STREAM,0);
if (client_fd==-1)
{
perror("socket");
return -1;
}
//绑定自身地址
memset(&addr,0,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;//ipv4
addr.sin_port=htons(8888);//端口
addr.sin_addr.s_addr=inet_addr("127.0.0.1");
//建立连接
connect(client_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr));
while(1)
{
memset(acBuf,0,1024);
scanf("%s",acBuf);
if (strcmp(acBuf, "exit" ) == 0)
{
break;
}
write(client_fd, acBuf, strlen(acBuf) );
memset(acBuf,0,1024);
read(client_fd,acBuf,1024);
printf("%s\n",acBuf);
}
//关闭文件描述符
close(client_fd);
return 0;
}
UDP服务器端
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int server_fd;
int nRead;
struct sockaddr_in server_addr={0};
struct sockaddr_in client_addr={0};
socklen_t addrlen;
server_fd=socket(PF_INET,SOCK_DGRAM,0);
if (server_fd==-1)
{
perror("socket");
return -1;
}
//绑定自身地址
memset(&server_addr,0,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(8888);
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");//server_addr.sin_addr.s_addr=INADDR_ANY;
nRead=bind(server_fd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr));
if(nRead==-1)
{
perror("bind");
}
char acBuf[1024]="";
while(1)
{
memset(acBuf,0,1024);
nRead=recvfrom(server_fd,acBuf,1024,0,(struct sockaddr *)&client_addr,&addrlen);
printf("%s\n",acBuf);
if (nRead==-1)
{
perror("recvfrom");
}
sendto(server_fd,acBuf,1024,0,(struct sockaddr *)&client_addr,sizeof(struct sockaddr));
}
return 0;
}
UDP客户端
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int client_fd;
struct sockaddr_in server_addr={0};
struct sockaddr_in client_addr={0};
socklen_t addrlen;
char acBuf[1024]={0};
client_fd=socket(PF_INET,SOCK_DGRAM,0);//数据报套接字
if (client_fd==-1)
{
perror("socket");
}
//地址
memset(&client_addr,0,sizeof(struct sockaddr_in));
client_addr.sin_family=AF_INET;
client_addr.sin_port=htons(8888);
client_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
while(1)
{
memset(acBuf,0,1024);
scanf("%s",acBuf);
if (strcmp(acBuf, "exit" ) == 0)
{
break;
}
sendto(client_fd,acBuf,1024,0,(struct sockaddr *)&client_addr,sizeof(struct sockaddr));
printf("waiting..\n");
memset(acBuf,0,1024);
recvfrom(client_fd,acBuf,1024,0,(struct sockaddr *)&server_addr,&addrlen);
printf("%s",acBuf);
}
close(client_fd);
return 0;
}