Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2006, 12:29 PM   #1
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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.
Polyphemus_ is offline   Reply With Quote
Old Feb 28th, 2006, 12:56 PM   #2
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
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.
jaeusm is offline   Reply With Quote
Old Feb 28th, 2006, 1:09 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Feb 28th, 2006, 2:01 PM   #4
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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?
Polyphemus_ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC