Information Exchange
I''ve programmed in WinSock a little client/server thingy. Anyway, when I try to send information beteww the two, I get some really weird, incorrect information send. For example, I try sending with:
send(SOCKET, "MyName", sizeof(that), MSG_OOB) and
recv(SOCKET, temp, sizeof(temp), MSG_OOB)
and, when I try to use what here would be temp, I get something like
8Þf
as the information which came across. I know this isn''t right (since no matter what I send, it''s the same information). Can anyone see what I''m doing wrong?
Don't worry, be random!
Get rid of MSG_OOB and your calls are incorrect, they should be:
const int BUFFER_IN_SIZE = 20;
char buffer_in[BUFFER_IN_SIZE];
memset(buffer_in, 0, BUFFER_IN_SIZE);
send(socket_out, "MyName", 6, 0);
recv(socket_in, buffer_in, BUFFER_IN_SIZE, 0);
<edit>I am assuming that his "temp" variable in the previous post, points to an array allocated on the heap.</edit>
assuming "temp" was a char array, using sizeof(temp) wouldn't return the number of chars in temp, it'd return the size of a pointer (typically 4-bytes now a-days.)
using strlen wouldn't work either because strlen searches for a NULL character to determine the length.
Hopefully this makes sense.
Regards,
Dire Wolf
www.digitalfiends.com
Edited by - Dire.Wolf on December 18, 2001 7:35:40 PM
const int BUFFER_IN_SIZE = 20;
char buffer_in[BUFFER_IN_SIZE];
memset(buffer_in, 0, BUFFER_IN_SIZE);
send(socket_out, "MyName", 6, 0);
recv(socket_in, buffer_in, BUFFER_IN_SIZE, 0);
<edit>I am assuming that his "temp" variable in the previous post, points to an array allocated on the heap.</edit>
assuming "temp" was a char array, using sizeof(temp) wouldn't return the number of chars in temp, it'd return the size of a pointer (typically 4-bytes now a-days.)
using strlen wouldn't work either because strlen searches for a NULL character to determine the length.
Hopefully this makes sense.
Regards,
Dire Wolf
www.digitalfiends.com
Edited by - Dire.Wolf on December 18, 2001 7:35:40 PM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
quote: Original post by Dire.Wolf
assuming "temp" was a char array, using sizeof(temp) wouldn''t return the number of chars in temp, it''d return the size of a pointer (typically 4-bytes now a-days.)
Tsk, tsk, tsk.
char buf[128];
char pBuf = buf;
sizeof( buf ) == 128
sizeof( pBuf ) == 4
-scott
No you misinterpreted what I meant.
Typically all my buffers are allocated on the heap using new . Therefore:
char *temp = new char[100];
sizeof(temp) == 4
I don't know about you but I typically avoid stack based arrays when using asynchronous I/O.
Sorry for the confusion.
Dire Wolf
www.digitalfiends.com
Edited by - Dire.Wolf on December 19, 2001 9:04:09 AM
Typically all my buffers are allocated on the heap using new . Therefore:
char *temp = new char[100];
sizeof(temp) == 4
I don't know about you but I typically avoid stack based arrays when using asynchronous I/O.
Sorry for the confusion.
Dire Wolf
www.digitalfiends.com
Edited by - Dire.Wolf on December 19, 2001 9:04:09 AM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
heh. Just clarifying through reprimand.
Besides - mine had a typo:
char pBuf;
should''ve been
char* pBuf;
...ah well.
-scott
Besides - mine had a typo:
char pBuf;
should''ve been
char* pBuf;
...ah well.
-scott
Yeah I saw that but didn''t want to get all nasty on you
Have a Merry Christmas,
Dire Wolf
www.digitalfiends.com
Have a Merry Christmas,
Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
quote: Original post by Dire.Wolf
I don''t know about you but I typically avoid stack based arrays when using asynchronous I/O.
It''s like you have an affinity for code that works or something...
Did you _want to send out-of-band data Fractile81?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
Well, I tried making these modifications and I''m still getting incorrect information. For example, when my client/server connects, the server registers it and then the client side starts to give the WinSock error that the socket is not initialized.
Here''s a code snippet for each:
int Client::Send(char *c)
{
if(send(s, c, strlen(c), 0) == SOCKET_ERROR) return 0;
// s here is the socket that the client is using
return 1;
}
// Buffer is of length BUFFER_SIZE... it is right
int Server::Receive(char *buf)
{
int n;
memset(buf, 0, BUFFER_SIZE);
if( (n=recv(s, buf, BUFFER_SIZE, 0)) == SOCKET_ERROR)
return 0;
// again, s here is a valid socket to the client
return n;
}
Again, any help is greatly appreciated! Thanks again!
~Fractile81
Don''t worry, be random!
Here''s a code snippet for each:
int Client::Send(char *c)
{
if(send(s, c, strlen(c), 0) == SOCKET_ERROR) return 0;
// s here is the socket that the client is using
return 1;
}
// Buffer is of length BUFFER_SIZE... it is right
int Server::Receive(char *buf)
{
int n;
memset(buf, 0, BUFFER_SIZE);
if( (n=recv(s, buf, BUFFER_SIZE, 0)) == SOCKET_ERROR)
return 0;
// again, s here is a valid socket to the client
return n;
}
Again, any help is greatly appreciated! Thanks again!
~Fractile81
Don''t worry, be random!
Don't worry, be random!
I'm assuming you are using TCP here.
Process #1: The Client
-----------------------
// initialize Winsock
// create socket
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(client == INVALID_SOCKET)
// error
// create and fill sockaddr_in struct
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1000); // for example
addr.sin_addr.s_addr = inet_addr("192.168.1.100"); // location of server
// connect to server
int result = connect(client, (sockaddr*)&addr, sizeof(addr));
if(result == SOCKET_ERROR)
// error
// create buffer
char buffer[500];
int n = send(client, buffer, sizeof(buffer), 0);
if(n == 0)
{
// connection closed
}
else if(n == SOCKET_ERROR)
{
// error
}
// data sent (not all of the data might be sent but I'm sure you know that)
Process #2: The Server
-----------------------
// initialize Winsock
// create socket
SOCKET server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(server == INVALID_SOCKET)
// error
// create and fill sockaddr_in struct
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1000); // for example
addr.sin_addr.s_addr = INADDR_ANY;
// bind socket to address
int result = bind(server, (sockaddr*)&addr, sizeof(addr));
if(result == SOCKET_ERROR)
// error
result = listen(server, 5);
if(result == SOCKET_ERROR)
// error
sockaddr_in incoming;
int incoming_len = sizeof(incoming);
memset(&incoming, 0, sizeof(incoming));
SOCKET client = accept(server, (sockaddr*)incoming, &incoming_len);
if(client == INVALID_SOCKET)
// error
// create buffer
char buffer[500];
int n = recv(client, buffer, sizeof(buffer), 0);
if(n == 0)
{
// connection closed
}
else if(n == SOCKET_ERROR)
{
// error
}
------------------
The code above should produce correct results (assuming that I didnt make any mistakes.) Obviously it is listed with a couple local variables that would be stored on the heap or in a class on the heap but you get the idea.
Dire Wolf
www.digitalfiends.com
Edited by - Dire.Wolf on December 20, 2001 1:59:41 PM
Process #1: The Client
-----------------------
// initialize Winsock
// create socket
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(client == INVALID_SOCKET)
// error
// create and fill sockaddr_in struct
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1000); // for example
addr.sin_addr.s_addr = inet_addr("192.168.1.100"); // location of server
// connect to server
int result = connect(client, (sockaddr*)&addr, sizeof(addr));
if(result == SOCKET_ERROR)
// error
// create buffer
char buffer[500];
int n = send(client, buffer, sizeof(buffer), 0);
if(n == 0)
{
// connection closed
}
else if(n == SOCKET_ERROR)
{
// error
}
// data sent (not all of the data might be sent but I'm sure you know that)
Process #2: The Server
-----------------------
// initialize Winsock
// create socket
SOCKET server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(server == INVALID_SOCKET)
// error
// create and fill sockaddr_in struct
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1000); // for example
addr.sin_addr.s_addr = INADDR_ANY;
// bind socket to address
int result = bind(server, (sockaddr*)&addr, sizeof(addr));
if(result == SOCKET_ERROR)
// error
result = listen(server, 5);
if(result == SOCKET_ERROR)
// error
sockaddr_in incoming;
int incoming_len = sizeof(incoming);
memset(&incoming, 0, sizeof(incoming));
SOCKET client = accept(server, (sockaddr*)incoming, &incoming_len);
if(client == INVALID_SOCKET)
// error
// create buffer
char buffer[500];
int n = recv(client, buffer, sizeof(buffer), 0);
if(n == 0)
{
// connection closed
}
else if(n == SOCKET_ERROR)
{
// error
}
------------------
The code above should produce correct results (assuming that I didnt make any mistakes.) Obviously it is listed with a couple local variables that would be stored on the heap or in a class on the heap but you get the idea.
Dire Wolf
www.digitalfiends.com
Edited by - Dire.Wolf on December 20, 2001 1:59:41 PM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement