|
without seeing the code that you are using in your receiver it is difficult to guess how to improve it.
|
|
|
|
|
Declare dynamic char buffer:
char* Dynamic_Recieve_Buffer = new char[2000];
Use dynamic buffer:
recv(mySocket , Dynamic_Receive_Buffer , 2000 , 0);
Example code:
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
char* Dynamic_Receive_Buffer = new char[2000];
WSADATA wsa;
SOCKET mySocket;
struct sockaddr_in server;
int recv_size;
char *message;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
if((mySocket = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
if (connect(mySocket , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
message = "GET / HTTP/1.1\r\n\r\n";
if( send(mySocket , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
puts("Data Send\n");
if((recv_size = recv(mySocket , Dynamic_Receive_Buffer , 2000 , 0)) == SOCKET_ERROR)
{
puts("recv failed");
}
puts("Reply received\n");
Dynamic_Receive_Buffer[recv_size] = '\0';
puts(Dynamic_Receive_Buffer);
return 0;
}
|
|
|
|
|
Can I find size of datatype without using sizeof operator ????
|
|
|
|
|
Not that I am aware of. What problem are you trying to solve?
|
|
|
|
|
How about this:
#define NO_SIZEOF_SIZEOF(type) ((long)(((type *)0) + 1))
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> If it doesn't matter, it's antimatter.<
|
|
|
|
|
Thanks, didn't think of that.
|
|
|
|
|
Even "youngsters" can have a few tricks up their sleeves.
I also wonder why the OP would need such a thing...maybe he's working with some stripped-down version of C for some egzotic platform that doesn't have sizeof...or it's a school-assignment-type question...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> If it doesn't matter, it's antimatter.<
|
|
|
|
|
Psst!
It's spelled "exotic."
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
what a perfect way!
|
|
|
|
|
Thanks
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> If it doesn't matter, it's antimatter.<
|
|
|
|
|
sizeof operator is the right way. I cannot see the reason for avoiding it.
Veni, vidi, vici.
|
|
|
|
|
He thinks he can make the wheel rounder...
|
|
|
|
|
Hi,
I have a loop for which I am tryig to connect on four ports for which I only get two
connections I know sometimes sockets take a while to connect
the Creation of the Socket and connection take place in CWinThread Dervived ::Initinstance after the CAsyncSocket objects have been created
Server is a Dos "C" program on the same Windows Vista machine
below is the loop
I am connected thru a router that is kind of old don't know if thats the issue
or If an imporvment in my hardware might help
I put my derived CAsynSocket class inside a a CWinThread class hoping that if such
issues arise I might raise the priority of the thread
looking for any pointers from the experts out there
// loop in CWinApp::Initinstance
for (i = 0, start_port = 11007; i < 4; start_port++, i++)
{
threadptr[i] = new SockCLeintThread(start_port);
if (threadptr[i] == NULL)
m_pMainWnd->MessageBox("SockClientThreadFail",NULL,MB_ICONERROR);
threadptr[i]->CreateThread(CREATE_SUSPENDED,0,NULL);
threadptr[i]->flags.is_connected = 0;
threadptr[i]->ipaddr = "192.168.1.4";
SetThreadName(threadptr[i]->m_nThreadID,thread[i]);
threadptr[i]->ResumeThread();
}
// My Casynsocket .h file
lass SockClient : public CAsyncSocket
{
DECLARE_SERIAL(SockClient)
public:
SockClient(UINT myport);
SockClient();
~SockClient();
char *sockbuffer; int num_buff;
CWnd *send_wnd;
int thread_no;
UINT port;
private:
int busy; protected:
virtual void OnReceive(int nErrorCode);
virtual void OnSend(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnClose(int nErrorCode);
}
IMPLEMENT_SERIAL(SockClient,CAsyncSocket,VERSIONABLE_SCHEMA | 2)
SockClient::SockClient()
{
sockbuffer = new char[300];
}
SockClient::SockClient(UINT myport)
{
port = myport;
sockbuffer = new char[300];
}
SockClient::~SockClient()
{
}
void SockClient :: OnConnect(int nErrorcode)
{
if(nErrorcode == 0)
Sockevent.SetEvent();
UNREFERENCED_PARAMETER(nErrorcode);
CAsyncSocket::OnConnect(nErrorcode);
busy = 1;
AsyncSelect(FD_WRITE);
}
void SockClient::OnClose(int nErrorCode) {
SockCLeintThread* current = dynamic_cast<SockCLeintThread*>(current);
current = (SockCLeintThread*)AfxGetThread();
current->flags.is_connected = 0;
CAsyncSocket::OnClose(nErrorCode);
}
void SockClient :: OnSend(int nErrorcode)
{
int bytes_sent;
if (num_buff > 0)
{
bytes_sent = Send(sockbuffer,num_buff,0);
num_buff = num_buff - bytes_sent;
CAsyncSocket::OnSend(nErrorcode);
AsyncSelect(FD_READ);
}
}
void SockClient::OnReceive(int nErrorcode)
{
static int i=0;
i++;
int nRead;
nRead = Receive(sockbuffer, 4096);
switch (nRead)
{
case 0:
Close();
break;
case SOCKET_ERROR:
if (GetLastError() != WSAEWOULDBLOCK)
{
AfxMessageBox ("Error occurred");
Close();
}
break;
default:
num_buff = nRead;
}
CAsyncSocket::OnReceive(nErrorcode);
send_wnd->SendMessage(WM_RECEIVE_SOCK_MESS,0,(LPARAM)sockbuffer);
}
class SockCLeintThread : public CWinThread {
public:
DECLARE_DYNCREATE(SockCLeintThread)
public:
SockCLeintThread(UINT myport);
SockCLeintThread();
~SockCLeintThread();
CWnd *sendwindow;
CWnd *call_wnd;
char *thread_id;
SockClient thisocket;
void Sendit(WPARAM, LPARAM);
void Receiveit(WPARAM, LPARAM);
LPCTSTR ipaddr;
BOOL idle; allexceptions myallexception;
struct {
unsigned int is_connected : 1;
unsigned int busy : 1;
} flags;
protected:
virtual BOOL InitInstance(); DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(SockCLeintThread,CWinThread)
BEGIN_MESSAGE_MAP(SockCLeintThread, CWinThread)
ON_THREAD_MESSAGE(WM_SEND_SOCK_MESS,Sendit)
END_MESSAGE_MAP()
void SockCLeintThread::Sendit(WPARAM count, LPARAM command)
{
UNREFERENCED_PARAMETER(command);
thisocket.num_buff = count;
thisocket.AsyncSelect(FD_WRITE);
return;
}
void SockCLeintThread::Receiveit(WPARAM count, LPARAM command)
{
UNREFERENCED_PARAMETER(count);
UNREFERENCED_PARAMETER(command);
thisocket.num_buff = 0;
}
SockCLeintThread::SockCLeintThread()
{
m_bAutoDelete = FALSE;
}
SockCLeintThread::SockCLeintThread(UINT myport) : thisocket(myport)
{
m_bAutoDelete = FALSE;
}
SockCLeintThread::~SockCLeintThread()
{
}
BOOL SockCLeintThread::InitInstance()
{
int error_code;
MSG m_msgCur;
extern void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName);
socklock.Lock();
if(thisocket.Create(thisocket.port,SOCK_STREAM,NULL)== 0)
{
error_code = GetLastError();
}
if (thisocket.AsyncSelect(FD_READ|FD_WRITE|FD_CONNECT|FD_CLOSE|FD_OOB|FD_ACCEPT) == 0)
error_code = GetLastError();
if( thisocket.Connect(ipaddr,thisocket.port) == 0)
{
error_code = GetLastError();
}
else
flags.is_connected = 1;
socklock.Unlock();
PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE);
return TRUE; }
|
|
|
|
|
Not sure what you're asking, this looks like a bit of a code dump. Typically connections won't take very long if you're within the same LAN, so you may have another issue with your code. By the way, if you're using an asynchronous socket, the connection is also asynchronous, meaning you don't sit there and wait for a response, the callback will be called when it's either connected or timed out.
ForNow wrote: PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE);
You don't have to manually create a message queue for a CWinThread.
|
|
|
|
|
You are right the CAsynSocket::OnConnect should be called by the framework one way or another at the very least with a WSAETIMEDOUT nErrorCode
I should put a TRACE in to printf the nerrorcode
|
|
|
|
|
Could you post some code?
|
|
|
|
|
I am recieving a buffer which is having unicode and ansi character. How can I read it so that I can get the actual string.
|
|
|
|
|
Please be a little bit more specific (e.g. by providing an example). Do you have a buffer that may contain an ANSI or Unicode (Microsoft wide character UTF16-LE, UTF16-BE, or UTF-8) string and you want to detect the encoding? If so, do you know the length of the buffer?
|
|
|
|
|
Yes you are right and I know the lenght of buffer.
|
|
|
|
|
Wide char strings stored in files usually begin with a BOM (Byte Order Mark)[^].
If there is no BOM in your case, use the ANSI strlen() function and compare the result with the buffer length without NULL bytes. If it matches, the string is ANSI or UTF-8. If not, it is probably a wide char string. For Microsoft Unicode (UTF-16LE), this can be verified by comparing the return value of wcslen() with the buffer length (multiply returned length by two / sizeof(wchar_t)).
This solution requires that the string in the buffer is NULL terminated. If not, create a new buffer with length + 2, copy the data and append two NULL bytes (two bytes to have a valid terminated wide char string).
If the strings in the buffer are NULL terminated, you may also look for two NULL bytes at the end of UTF-16 strings.
|
|
|
|
|
Thanks for your help but I dont know how to check BOM..can you show by example...
|
|
|
|
|
Detecting code page from BOM:
UINT nCP = 0; if (0 == memcmp(lpszBuf, "\xEF\xBB\xBF", 3))
nCP = CP_UTF8;
else if (0 == memcmp(lpszBuf, "\xFF\xFE", 2))
nCP = 1200; else if (0 == memcmp(lpszBuf, "\xFE\xFF", 2))
nCP = 1201;
However, this requires the presence of a BOM. With text files, there should be one for UTF-16 and may be one with UTF-8. If you get your data from elsewhere (e.g. as network package), there may be no BOM.
|
|
|
|
|
It will comapare the byte with 0xFFFE, 0xFEFF but my problem is to seperate out ANSI and UNICODE string from a BYTE buffer.
My buffer will will be like "ConnectInfo?ã„·ã„´??56.58.98.142?fgg?n?v HTT". In this buffer unicode data is "ã„·ã„´" and remaining data is in ANSI.
I need to seperate out the data and need to create a complete string of unicode characters.
|
|
|
|
|
That's why I asked for an example in my first post:
Your string seems to be UTF-8 encoded.
To decode it you can use MultiByteToWideChar() which converts it to a wide char string:
int nWideLen = ::MultiByteToWideChar(CP_UTF8, 0, lpszBuf, nLen, NULL, 0);
wchar_t *lpszWide = new wchar_t[nWideLen];
::MultiByteToWideChar(CP_UTF8, 0, lpszBuf, nLen, lpszWide, nWideLen);
delete [] lpszWide;
|
|
|
|
|
execuse me. where can I get some data about encoding. actualy, I don't know exactly what's the different between UTF8 and UNICODE. Thank you very much
|
|
|
|
|