TCPServer
2015-08-27 02:36
661 查看
// Server.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <winsock2.h> #include <fcntl.h> #include <windows.h> #include <io.h> #include <sys\stat.h> #include <iostream> using namespace std ; #define FILENAME "./a.bin" #pragma comment(lib, "ws2_32.lib") int getfilesize(const char* p) { FILE *fp = fopen(p,"r"); if(fp) { fseek(fp, 0, SEEK_END); int size = ftell(fp); fclose(fp); return size; } return 0 ; } class NetComm { public: explicit NetComm( const char * ip , int port ) { WSADATA Ws; WSAStartup(MAKEWORD(2,2), &Ws) ; server_sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if( server_sockfd == INVALID_SOCKET) { printf("create failed\n" ) ; exit(0); } server_address.sin_family = AF_INET; /*命名套接字*/ server_address.sin_addr.s_addr = inet_addr(ip); server_address.sin_port = htons(port ); server_len = sizeof(server_address); if ( bind(server_sockfd,(struct sockaddr *)&server_address,server_len) != 0 )/*绑定套接字*/ { printf("bind failed\n" ) ; exit(1) ; } listen(server_sockfd,5); } const int OpenCursor() { while(1) { printf("%d Server waiting !\r\n" ,__LINE__); client_len = sizeof(client_address); client_sockfd = ::accept(server_sockfd,(struct sockaddr *)&client_address,(int*)&client_len); /*接收一个客户连接*/ int Data = 0 ; char *pdata= (char*) &Data ; char ch[4] = {0 } ; int sizeRead = 0 ; while( sizeRead < 4 ) { memset(ch,0x00,sizeof(ch) ) ; int isize = ::recv(client_sockfd , ch , 4 , 0 ) ; printf("[%d] character receieved,[%s]\n" ,isize , ch) ; memcpy(pdata , ch ,isize ) ; pdata += isize ; sizeRead += isize ; } printf("data = %d\n" , Data ) ; sizeRead=0; char chBuf[1024] = {0} ; char *pBuf = chBuf ; char chSize[1024] = {0}; while(sizeRead<Data) { memset(chSize,0x00,sizeof(ch) ) ; int isize = ::recv(client_sockfd , chSize , Data , 0 ) ; printf("[%d] character receieved,[%s]\n" ,isize , chSize) ; // printf(ch); memcpy(pBuf , chSize ,isize ) ; pBuf += isize ; sizeRead += isize ; } printf("%s\n" , chBuf ) ; break; } printf("************************************\n") ; unsigned char ctype=0; ::send(client_sockfd ,(const char*)&ctype , 1 , 0 ) ; return 0; } const int Fetch() { do { printf("Server waiting !\r\n"); unsigned short i=0xFDF4; send(client_sockfd,(const char*)&i,sizeof(unsigned short ) , 0 ); unsigned char type = 1 ; send(client_sockfd,(const char *)&type , 1 , 0 ) ; unsigned int lowSize = getfilesize(FILENAME); printf("lowSize=%d\n" , lowSize) ; send(client_sockfd,(const char*)&lowSize,4 , 0 ); HANDLE fileHandle = CreateFile(FILENAME,GENERIC_READ | GENERIC_WRITE , 0,0,OPEN_ALWAYS,0,0); HANDLE hMapping = CreateFileMapping(fileHandle,0,PAGE_READONLY,0, lowSize,"CSFlash") ; LPSTR lpData = (LPSTR)MapViewOfFile(hMapping,FILE_MAP_READ,0,0,lowSize); printf("lpD 4000 ata=%s\n" , lpData ) ; send(client_sockfd,(const char*)lpData,lowSize , 0 ); unsigned char reserver = 0; send(client_sockfd,(const char*)&reserver,sizeof(char) , 0 ); unsigned short tail=0xFDF5; send(client_sockfd,(const char*)&tail,sizeof(tail) , 0); }while(0) ; return 0 ; } private: int server_sockfd,client_sockfd; unsigned int server_len,client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; } ; int main(int argc,char *argv[]) { NetComm oNetComm(argv[1],atoi(argv[2])) ; for(;1;) { oNetComm.OpenCursor(); oNetComm.Fetch(); } return 0; }
相关文章推荐
- 字节型TCP协议解析
- scrapy 爬取https网页时出现ssl错误
- Android HTTP session && cookie
- Android 通过httpclient请求web服务器,并解决用户登录session保持
- Http Basic Authentication 的请求头Authorization的状态保存问题:见‘斜粗下划线’部分说明
- Http Basic Authentication has some limitations, maybe nginx could do some help...
- ub 网络框架的几种线程模型
- PostgreSQL之网络地址类型
- 网络配置
- Httpd运维日志:通过apxs添加模块
- 2015年8月15-16日,杨学明老师《互联网行业市场管理与产品规划》内训在中国科学院互联网络信息中心成功举办!
- HelloX操作系统网络功能简介及使用和开发指南
- HelloX操作系统网络功能简介及使用和开发指南
- TCP实现P2P通信、TCP穿越NAT的方法、TCP打洞
- TCP/IP
- C# httpwebrequest访问HTTPS错误处理
- TCP/IP详解-坚持定时器和保活定时器
- Kids and Prizes 来源: <http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85996#problem/B>
- HttpClient4.3教程
- 《HTTP权威指南》阅读笔记(九)