您的位置:首页 > 理论基础 > 计算机网络

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: