Re: Socket Communication with Laptop
- From: "Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT com>
- Date: Thu, 20 Jul 2006 11:14:28 -0700
I'd write my protocol so you know how much data is coming ahead of time,
then keep receiving until that many bytes have arrived and then stop. How
many more bytes are currently available in the socket is *not* an indication
of how many total are coming. That completely depends on how the sender
puts the data into the stream and whether there are any network delays
before you get the packets. The size of packets sent is also pretty much
unrelated to how much data is passed to send() on the sender's side.
Packetization is done by the TCP/IP stack.
Paul T.
"Vin" <Vin@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:8EB2D20C-4C02-4943-A519-2D8DC40E44D8@xxxxxxxxxxxxxxxx
Hi Paul,
Thanks for the response. I am new so, just learning here.
The laptop seems to be sending at 1024 for a 1mb file. I use ioctlsocket()
to determine if there is more data in the socket buffer to receive.
So, how can you tell if there is more data on socket to be received?
Thanks
"Paul G. Tobey [eMVP]" wrote:
The type of wireless connection is completely irrelevant, but you haven't
told us the *speed* of the wireless connection, which it should be
obvious
*does* matter. You've shown most of your code, but left out all of the
critical pieces. How big is the buffer into which your file data is
received? Why are you calling ioctlsocket() every time through the read
loop?
Paul T.
"Vin" <Vin@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:D5FAE607-84CE-4AD2-A9D7-3BF568BF86CF@xxxxxxxxxxxxxxxx
Hi,
Again, I am fairly new at EVC 4.0, so please help me.
I am trying to send a file (size: 1MB) from the a laptop to ppc using
wireless connections (adhoc).
I am able to send the file to the ppc, but it takes about 3 minutes.
Why?
On
the ppc, I would send a message to the laptop, and the laptop would
send
the
file to the ppc. The laptop is using IP = 192.168.1.1 for laptop and
PORT
7001.
here is my code snippet:
SOCKET WinSocket = INVALID_SOCKET;
DWORD dwDestAddr;
SOCKADDR_IN sockAddrDest;
SOCKET sockDest;
WSADATA WSAData;
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
return false;
//create socket
sockDest = socket(AF_INET, SOCK_STREAM, 0);
if (sockDest == SOCKET_ERROR)
{
WSACleanup ();
return false;
}
//convert address to in_addr (binary) form
dwDestAddr = inet_addr(serverIP);
// initialize SOCKADDR_IN with IP address
//port number and address family
memcpy(&sockAddrDest.sin_addr,&dwDestAddr, sizeof(DWORD));
sockAddrDest.sin_port = htons(SOCKET_PORT);
sockAddrDest.sin_family = AF_INET;
//attempt to conenct to server
if (connect(sockDest,(LPSOCKADDR)&sockAddrDest,sizeof(sockAddrDest)) ==
SOCKET_ERROR)
{
DWORD err = WSAGetLastError();
WSACleanup ();
return false;
}
if (send(sockDest, sendData, 12, 0)== SOCKET_ERROR)
{
closesocket (sockDest);
WSACleanup ();
return false;
}
int rc;
FD_ZERO(&master_set);
FD_SET(sockDest, &master_set);
struct timeval timeout;
timeout.tv_sec = 10;
//20 secs wait for socket to alert if anything ready to receive
//set socket receive buf size option
setsockopt(sockDest, SOL_SOCKET, SO_RCVBUF, (char*)&optVal, optLen);
//create file to save
HANDLE evlFile;
evlFile = CreateFile(_T("/TmpFile.dat"),
GENERIC_READ | GENERIC_WRITE, //access Options
FILE_SHARE_READ, //share options
NULL,//ignored
CREATE_ALWAYS, //creation options
0, //file attributes
NULL);
if (evlFile == INVALID_HANDLE_VALUE)
{
CloseHandle(evlFile); //close handle to file
closesocket (sockDest);
WSACleanup ();
return false;
}
//receive and verify that we have event information to write to
tmpEvent.evt
file
//wait til socket says ok to receive
rc = select(0, &master_set, NULL, NULL, &timeout);
if(rc == 1)
{
//ok..socket says ok to receive now
while (rvSize != 0)
{
rv = ioctlsocket(sockDest, FIONREAD, (u_long FAR*) &iMode);
if(iMode == 0)
break;
rvSize = recv (sockDest, (char*)sbuf, size, 0);
if(rvSize > 0)
{
//write data for BTL File
WriteFile(evlFile, sbuf, rvSize, &dwWrite, NULL);
}
}
}
CloseHandle(evlFile);
// Disable sending on ServerSock.
shutdown (sockDest, 0x01);
// Disable receiving on ServerSock.
shutdown (sockDest, 0x00);
.
- Follow-Ups:
- Re: Socket Communication with Laptop
- From: Vin
- Re: Socket Communication with Laptop
- References:
- Socket Communication with Laptop
- From: Vin
- Re: Socket Communication with Laptop
- From: Paul G. Tobey [eMVP]
- Re: Socket Communication with Laptop
- From: Vin
- Socket Communication with Laptop
- Prev by Date: Re: Socket Communication with Laptop
- Next by Date: Re: Error installing eMbedded Visual C++ 4.0
- Previous by thread: Re: Socket Communication with Laptop
- Next by thread: Re: Socket Communication with Laptop
- Index(es):
Relevant Pages
|