6.1.2Linux下Socket编程
2016-04-08 16:50
169 查看
tcp简单实验
server.c
server.c
#include<sys/types.h>/*SeeNOTES*/ #include<sys/socket.h> #include<string.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<unistd.h> #include<stdio.h> #include<signal.h> /*socket *bind *listen *accept *send/recv */ #defineSERVER_PORT8888 #defineBACKLOG10 intmain(intargc,char**argv) { intiSocketServer; intiSocketClient; structsockaddr_intSocketServerAddr; structsockaddr_intSocketClientAddr; intiRet; intiAddrLen; intiRecvLen; unsignedcharucRecvBuf[1000];//接收缓冲区 intiClientNum=-1; signal(SIGCHLD,SIG_IGN);//等待为子进程收尸信号 /*AF_INET为ipv4类型,SOCK_STREAM为Tcp连接类型,*/ iSocketServer=socket(AF_INET,SOCK_STREAM,0); if(-1==iSocketServer) { printf("socketerror!\n"); return-1; } tSocketServerAddr.sin_family=AF_INET; tSocketServerAddr.sin_port=htons(SERVER_PORT);/*hosttonet,short*/ tSocketServerAddr.sin_addr.s_addr=INADDR_ANY;/*本机上的所有IP*/ memset(tSocketServerAddr.sin_zero,0,8);/*设置无用的空间为0*/ //配置本地信息
iRet=bind(iSocketServer,(conststructsockaddr*)&tSocketServerAddr,sizeof(structsockaddr));
if(-1==iRet)
{
printf("binderror!\n");
return-1;
}
//最大监听个数BACKLOG
iRet=listen(iSocketServer,BACKLOG);
if(-1==iRet)
{
printf("listenerror!\n");
return-1;
}
while(1)
{
iAddrLen=sizeof(structsockaddr);
iSocketClient=accept(iSocketServer,(structsockaddr*)&tSocketClientAddr,&iAddrLen);
if(-1!=iSocketClient)
{
iClientNum++;
printf("Getconnectfromclient%d:%s\n",iClientNum,inet_ntoa(tSocketClientAddr.sin_addr));
if(!fork())
{
/*子进程的源码*/
while(1)
{
/*接收客户端信息并打印出来*/
iRecvLen=recv(iSocketClient,ucRecvBuf,999,0);
if(iRecvLen<=0)
{
close(iSocketClient);
return-1;
}
else
{
ucRecvBuf[iRecvLen]='\0';
printf("GetMsgFromClient%d:%s\n",iClientNum,ucRecvBuf);
}
}
}
}
}
close(iSocketServer);
return0;
}
client.c
#include<sys/types.h>/*SeeNOTES*/
#include<sys/socket.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdio.h>
#include<signal.h>
/*socket
*connet
*send/recv
*/
#defineSERVER_PORT8888
intmain(intargc,char**argv)
{
intiSocketClient;
structsockaddr_intSocketServerAddr;
intiRet;
unsignedcharucSendBuf[1000];
intiSendLen;
if(argc!=2)
{
printf("Usage:\n");
printf("%s<server_ip>\n",argv[0]);
return-1;
}
iSocketClient=socket(AF_INET,SOCK_STREAM,0);
tSocketServerAddr.sin_family=AF_INET;
tSocketServerAddr.sin_port=htons(SERVER_PORT);/*hosttonet,short*/
if(0==inet_aton(argv[1],&tSocketServerAddr.sin_addr))/*本机上的所有IP*/
{
printf("invalidserver_ip\n");
return-1;
}
memset(tSocketServerAddr.sin_zero,0,8);/*设置无用的空间为0*/
iRet=connect(iSocketClient,(structsockaddr*)&tSocketServerAddr,sizeof(structsockaddr));
if(iRet==-1)
{
printf("connecterror!\n");
return-1;
}
while(1)
{
if(fgets(ucSendBuf,999,stdin))
{
iSendLen=send(iSocketClient,ucSendBuf,strlen(ucSendBuf),0);
if(iSendLen<=0)
{
close(iSocketClient);
return-1;
}
}
}
return0;
}
相关文章推荐
- Linux/UNIX线程(2)
- 在CentOS上安装Java环境:使用yum安装java
- linux添加网关和出接口不同网段的路由
- CentOS下载
- VMware虚拟的CENTOS系统中挂在CD/DVD驱动
- Linux内核及分析 第七周 可执行程序的装载
- Linux 应用编程
- linux之NAND FLASH驱动程序
- linux之keepalived详解
- [原创] ubuntu下安装scrapy报错 error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
- Linux命令
- linux下各种解压,压缩命令
- 卸载CentOS7-x64自带的OpenJDK并安装Sun的JDK8的方法
- CentOS6.5编译安装最新MySQL 5.7.11
- linux下Yum的$releasever和$basearch的取值
- Linux下DIR,dirent,stat 结构体
- 安装Linux系统后调优及安全设置
- 远程访问服务器上的mySQL -- Linux环境
- CentOs conn mysql
- Windows下Linux 环境 Cygwin安装及配置 基本工具使用