![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Socket problem
Hi,
Currently, I'm experimenting a bit with sockets. Currently, I'm writing a client-server thingy, using the TCP protocol and WinSock 2. I'm not using text, but binary. A problem has popped up a few days ago. If a specific part of the data sent, a number of structs, it is only received half of the time. This is weird, I thought TCP had no dataloss, especially not when connecting through localhost? What basically happens is that the first byte of the block of memory the socket reads to is written over, but the bytes after that are left the way they are. The recv function unblocks though. I won't post the code which sends the data (I'm quite sure the error wouldn't be in there, and it is just too much), but maybe there is something wrong in my initialization code? Client side: /* Create a socket */
aSocket::aSocket(const char *host, bInt port) {
if (!socketsInit)
initSockets();
int nret;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(port);
sockaddr.sin_addr.s_addr = inet_addr(host);
nret = connect(sock, (LPSOCKADDR) &sockaddr, sizeof(SOCKADDR_IN));
if (nret != 0) {
bError("Could not connect to server");
}
}Server side: /* Creates a server socket */
aServerSocket::aServerSocket(int port) {
this->port = port;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // init socket
if(sock == INVALID_SOCKET) { /* check socket is valid */
bErrorF("Socket failed, error code %i", WSAGetLastError());
return;
}
/* bind server socket to a port */
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(port);
int nret = bind(sock, (LPSOCKADDR) &serverInfo, sizeof(struct sockaddr));
if(nret == SOCKET_ERROR) {
bErrorF("Socket failed, error code %i", WSAGetLastError());
return;
}
if(listen(sock, 10) != 0) {
bError("listen() failed");
return;
}
}My excuses for the bad commenting. If there is need for it, I'll comment it. Thanks in advance, - Polyphemus Last edited by Polyphemus_; Feb 28th, 2006 at 12:48 PM. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
I only do socket programming in Java, but one possible reason this may be happening is that you're not flushing your output buffer. I've never used C++ for network programming, so I can't tell you for sure. In Java, when you send data from output stream (like PrintWriter) associated with a socket, you sometimes need to call the flush() method to force the data out of the buffer. Otherwise, it will appear that the data was not received, when it was actually never sent.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
So it doesn't give you any errors? I don't see the problem here, but I might be missing it.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
I created a fix, but I only want to use this temporarily... putting a Sleep(1); before I receive the message fixes it
I wonder why this helps? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|