Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ socket
this is server
 
#pragma once
#include <WINSOCK2.H>  
#pragma comment(lib,"Ws2_32.lib")  
 
#define PORT 8001
#define IP "127.0.0.1"
#define SINGEL_SEND "single_send"
 
class CSock
{
private:
	SOCKET Sock_Server;
	SOCKET Sock_Client;
	WSADATA Wsa;
	SOCKADDR_IN AddrServer;
	SOCKADDR_IN AddrClient;
	int LenOfAddrClient ; 
	int sendTotal;
	int sendThisTime;
	char buff[20];
public:
	bool senddata(char * data , int nsize);
	bool sockini();
	void recvData( char *data , int nsize ) ;
	bool recvFlag();
	CSock(void);
	~CSock(void);
};
 

 

#include "Sock.h"

 

CSock::CSock(void)
{
}
 

CSock::~CSock(void)
{
	closesocket(Sock_Server);
	WSACleanup() ; 
}
 
bool CSock::senddata(char * data , int nsize)
{
	sendTotal = 0 ;
	sendThisTime = 0 ;
	while ( sendTotal < nsize )
	{
		sendThisTime = send( Sock_Client , data + sendTotal , nsize - sendTotal , 0  );
		sendTotal += sendThisTime ;
		if (sendThisTime < 0 )
		{
			return false;
		}
	}
	return true ;
}
 
void CSock::recvData( char *data , int nsize )
{
	sendTotal = 0 ;
	sendThisTime = 0 ;
	while ( sendTotal < nsize )
	{
		sendThisTime =  recv( Sock_Client , data+sendTotal , nsize - sendTotal , 0  );
		sendTotal += sendThisTime ;
	}
}
 
bool CSock::recvFlag()
{
	memset( (void*)buff , 0 , strlen( SINGEL_SEND ) );
	recvData( buff , strlen(SINGEL_SEND));
	if ( memcmp( (void*)buff , SINGEL_SEND , strlen(SINGEL_SEND) ) == 0 )
	{
		return true ;
	}
	return false ;
}
 
bool CSock::sockini()
{
	if (WSAStartup( MAKEWORD(2,2),&Wsa ))
	{
		return false;
	}
	Sock_Server = socket( AF_INET , SOCK_STREAM , 0 );
	if ( Sock_Server == INVALID_SOCKET  )
	{
		WSACleanup();
		return false ;
	}
	AddrServer.sin_family = AF_INET ; 
	AddrServer.sin_port = htons( PORT );
	AddrServer.sin_addr.S_un.S_addr = inet_addr( IP );
 
	if ( bind( Sock_Server , (SOCKADDR*)&AddrServer , sizeof(AddrServer) ) )
	{
		closesocket( Sock_Server );
		WSACleanup();
		return false ;
	}
	
	if ( listen( Sock_Server , 5 )  )
	{
		WSACleanup() ; 
		return false;
	}
	LenOfAddrClient = sizeof(AddrClient);
 
	Sock_Client =  accept( Sock_Server , (SOCKADDR*)&AddrClient , &LenOfAddrClient );
 

	return true;
}
 

//这里是main()函数
#include <iostream>
#include "Sock.h"
#include <WINSOCK2.H>
#pragma comment(lib,"Ws2_32.lib")
#define SINGEL_SEND "single_send"
using namespace std;
 
char buff[20];  //recv;

 
//定义接受压缩图片的信息 
typedef struct bmpheader
{
	int indexBitmap;  //图片索引号
	int x , y ;          //图片的   横向、纵向  块号
	int nLength ;    //块压缩之后的大小
	int nSum ;       //计数块
	int nDisplay ;
}BMPHEADER , *pBMPHEADER;
 
//图片第一次发送的相关信息 
typedef struct bitinfo
{
	int bitWidth;       // 屏幕的宽度 
	int bitHeight;		//屏幕的高度 
	int bitCount;		//位图的大小 
	int bitSize;		// 图片压缩后的大小 
}BITINFO,*PBITINFO;
 

int main(int argc, char* argv[])
{
 
	CSock socks;
 
	if(socks.sockini() == 0 )
	{
		cout<<"初始服务端失败!";
	}
 

	BITINFO bit;
	bit.bitCount = 24 ;
	bit.bitHeight = 1366 ; 
	bit.bitWidth = 768 ; 
	bit.bitSize = 255 ;
 

	BMPHEADER bmp;
	ZeroMemory( (void*)&bmp , sizeof(BMPHEADER) );
 
	while (1)
	{
		bmp.indexBitmap++;
		bmp.nLength = sizeof(BMPHEADER);
		socks.recvFlag() ;
		bmp.nDisplay = 1 ;
		socks.senddata((char*)&bmp , sizeof(BMPHEADER)  ) ;
	}
	
	return 0;
}
 

 
Send data when the memory is increasing, do not know why
 
[EDIT: Copied text posted as solution]
i found no problem in my program , but i dont know why it cost me about 4M memory
[/EDIT]
Posted 22-Nov-12 20:09pm
Edited 27-Nov-12 23:05pm
v2
Comments
Richard MacCutchan at 24-Nov-12 4:48am
   
Your while() loop runs forever.
midle110 at 24-Nov-12 21:12pm
   
while() is for testing ,no problem

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this senddata function, I have not tested but give it a try.
bool CSock::senddata(char * data , int nsize)
{
      bool bRet = false;
      sendThisTime = send( Sock_Client, data, sizeof(BMPHEADER), 0  );
    if (sendThisTime > 0) {
       bRet = true;
    }
    return (bRet) ;
}
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100