• TCP Server—Linux


    #include <stdio.h>
    #include <netinet/ip.h>
    
    #define BUFF_SIZE 1024
    
    int main(int argc,char *argv[])
    {
    
    	int a = 0;
    	int iSock = 0, tmpSock = 0;
    	int opt = 0;
    	int iRet = 0;
    	int iRes = 0;
    	int iFileDesc = 0;
    	int iFileRet = 0;
    	unsigned short usPeerPort = 11000;
    	char *pPeerIP = "192.168.1.100";
    	unsigned char pRecvBuff[20480];
    	unsigned char pSendBuff[20480] = "I'm Zynq ^_^";
    	time_t now;
    	struct tm *p;
    	unsigned int uiCnt = 0;
    	unsigned int uiPeriod = 0;
    	unsigned int uiSpeed = 0;
    	unsigned int uiTmp = 0;
    	char filePre[20] = {0};
    	char fileName[100] = {0};
    	char cmd[100] = {0};
    	struct sockaddr_in addrLocal, addrPeer;
    	static unsigned int s_uiLen = 0;
    
    
    	memset(pRecvBuff, 0x00, 1024);
    
    	iSock = socket(AF_INET, SOCK_STREAM, 0);
    	if (iSock < 0)
    	{
    		printf("Create socket error![%d]
    ", iSock);
    		return 0;
    	}
    
    	//iRes = 1;
    	//ioctlsocket(iSock, FIONBIO, (u_long FAR*)&iRes);
    
    	/* 设置本地的IP地址 */
    	addrLocal.sin_family = AF_INET;
    	addrLocal.sin_port = htons(8960);
    	addrLocal.sin_addr.s_addr = htonl(INADDR_ANY);
    	//addrLocal.sin_addr.S_un.S_addr = inet_addr("10.10.22.100");
    
    	///* 设置远端的IP地址 --by cqs */
    	//addrPeer.sin_family = AF_INET;
    	//addrPeer.sin_port = htons(usPeerPort);
    	//addrPeer.sin_addr.s_addr = inet_addr(pPeerIP);
    
    	iRet = bind(iSock, (struct sockaddr *)&addrLocal, sizeof(struct sockaddr));
    	if (iRet < 0)
    	{
    		printf("bind error
    ");
    		return 0;
    	}
    
    	iRet = listen(iSock, 5);
    	if (iRet < 0)
    	{
    		printf("listen error
    ");
    		return 0;
    	}
    
    	opt = sizeof(struct sockaddr);
    	tmpSock = accept(iSock, (struct sockaddr *)&addrPeer, &opt);
    	if (tmpSock > 0)
    	{
    		printf("accepted %d.
    ", tmpSock);
    	}
    	else
    	{
    		printf("accepted error %d.
    ", tmpSock);
    	}
    
    	while(1)
    	{
    		iRet = recv(tmpSock, pRecvBuff, BUFF_SIZE, 0);
    		if (iRet > 0)
    		{
    			printf("%s.
    ", pRecvBuff);
    					
    		}
    	}
    
    
    	close(tmpSock);
    	close(iSock);
    
    	return 0;
    }
    

      

  • 相关阅读:
    实现JDK代理
    Dictionary字典类介绍
    在服务器上新建虚拟机及安装系统
    开启远程桌面设置
    Windows Server 2008 R2远程协助选项灰色
    server 2012系统更改电脑密码
    eclipse + maven + scala+spark环境搭建
    C#数据路接口中获取SQL数据的用法
    C#常用快捷键
    oracle常用的快捷键
  • 原文地址:https://www.cnblogs.com/cnpirate/p/3480774.html
Copyright © 2020-2023  润新知