LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2003, 02:09 PM   #1
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Rep: Reputation: 0
Unhappy Server/Client-commucation via TCP-Sockets


Hey, there!

Well, I wrote two classes encapsulating socket funcitons of winsock1 (what should run under unix, too). I'm bored of that MS socket stuff. However, the server-application runs as exspected. The client-app's Connect()-function seems to fail, but the running server-app receives the connection. The client's Connect()-functions is implemented as follows:
Code:
bool CClient::Connect(std::string strServerIP, unsigned int nPort){
	if(m_bIsConnected)
		Close();

	memset(&m_SockAddr, 0, sizeof m_SockAddr);
	m_SockAddr.sin_family = AF_INET;
	m_SockAddr.sin_addr.s_addr = inet_addr(strServerIP.c_str());
	m_SockAddr.sin_port = htons(nPort);

	OnConnecting();
	int nResult = connect(m_nSocket, (sockaddr *)&m_SockAddr, sizeof m_SockAddr);
	if(m_bIsConnected = (nResult != SOCKET_ERROR)){
		OnConnect();
		Resume();
	}

	return m_bIsConnected;
}
The function alsways returns false by connecting to the server at 127.0.0.1:23. I have no clue of how nResult becoming -1 (SOCKET_ERROR) but enabling the connection...
I hope one could help me out.
BEst regards, Gary
 
Old 05-12-2003, 04:02 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
First of all, what's the physical relationship
between your client & server? Do they run
on the same machine? Can you telnet the box?


Cheers,
Tink
 
Old 05-12-2003, 04:09 PM   #3
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Original Poster
Rep: Reputation: 0
I forst wrote the server application. It does run on the same machine and everything works fine if i telnet to my "server". My "client" is compareable to telnet. It should just connect to the server, output every received data and send all input data directly. This is totally protocol-independend.
Again: telneting works fine, but the problem I have on the client side is: connect() fails, but the server receives a connection. But how/why?
 
Old 05-13-2003, 06:12 PM   #4
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Original Poster
Rep: Reputation: 0
Detect server's disconnection

Well, I figured out that connect() always fails if the unconnected socket is in non-blocking mode. So I set in to non-blocking mode after connection. Now the server-client-connections works all fine except of I have no clue how to recognize the server's disconnection. If the server closes its socket, the non-blocking client still returns -1 on read() meaning no data available. It never returns 0.
On the other hand: if the client closes the connection, the read()-method on the server side returns 0 indicating the client's closure. But that procedure does not work on the client side in case of a server's socket closure
Maybe there is some bug in my code I do not know about, cause this is my first socket encapsulation. But referring to all used documentation I think it's ok like that... However: if anybody would like to see my code: email me cause my webspace-provider does not support http-downloads . Remember: I used winsock1. If you're gonna test it under unix, please include the corresponding headers yourself. Thx.
I'd appreciate any kind of constructive critic/help.

Gary

Last edited by cYbORg; 05-13-2003 at 06:18 PM.
 
Old 05-18-2003, 06:39 AM   #5
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Rep: Reputation: 30
I've been following this nice tutorial on the subject:

http://www.ecst.csuchico.edu/~beej/g....html#blocking

You might want to check out that select() function.
 
Old 05-18-2003, 06:01 PM   #6
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Original Poster
Rep: Reputation: 0
Well, I dont's think that's the answer to my question. My problem is the client announcing a connecton failure but the server recognizes a new connection. That does not have anything to do with checks on data availability.
There are many tutorials on the web but none of 'em tells anything about the descripted behaviour

So far, Gary
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
client/server question (with sockets) bicycle Programming 1 11-24-2005 01:26 PM
Sockets Programing Client/Server ashucool83 Programming 9 09-30-2005 02:43 PM
TCP Network Sockets Rakoon Red Hat 1 08-04-2005 12:36 AM
Tcp/ip Client Server Problem tushar123 Programming 2 03-31-2004 05:16 PM
Maximum TCP-Sockets Quis Linux - Networking 2 01-17-2003 03:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration