Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Fractile81

Information Exchange

This topic is 6027 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
heh. Just clarifying through reprimand.

Besides - mine had a typo:

char pBuf;

should''ve been

char* pBuf;

...ah well.

-scott

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!