www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Sockets issue

reply okibi <okibi ratedo.com> writes:
I'm a bit puzzled here. I'm trying to get my sockets working, but it only
connects once. Here is my code:

server.d:
auto soc = new TcpSocket();
soc.bind(new InternetAddress("localhost", 10101));
soc.listen(10);
Stream str = new SocketStream(soc.accept());
while(true)
{
	char[] line = str.readLine();
	if (line !is null)
		cmdOpen(line);
}

client.d:
Socket socket = new TcpSocket(new InternetAddress("localhost", 10101));
Stream stream = new SocketStream(socket);
stream.writeString(args[1]);
socket.close();

Can anyone point out what I'm doing wrong? The first connection works fine, but
any connection afterwords fails (line IS null on second connection).

Thanks!
Jan 15 2008
parent reply Raynor <memphis007fr yahoo.fr> writes:
okibi a écrit :
 I'm a bit puzzled here. I'm trying to get my sockets working, but it only
connects once. Here is my code:
 
 server.d:
 auto soc = new TcpSocket();
 soc.bind(new InternetAddress("localhost", 10101));
 soc.listen(10);
 Stream str = new SocketStream(soc.accept());
 while(true)
 {
 	char[] line = str.readLine();
 	if (line !is null)
 		cmdOpen(line);
 }
 
 client.d:
 Socket socket = new TcpSocket(new InternetAddress("localhost", 10101));
 Stream stream = new SocketStream(socket);
 stream.writeString(args[1]);
 socket.close();
 
 Can anyone point out what I'm doing wrong? The first connection works fine,
but any connection afterwords fails (line IS null on second connection).
 
 Thanks!
while (true) { Socket clientSocket = soc.accept(); Stream str = new SocketStream(clientSocket); while(clientSocket.isAlive()) { char[] line = str.readLine(); if (line !is null) cmdOpen(line); } }
Jan 15 2008
parent reply okibi <okibi ratedo.com> writes:
Raynor Wrote:

 okibi a écrit :
 I'm a bit puzzled here. I'm trying to get my sockets working, but it only
connects once. Here is my code:
 
 server.d:
 auto soc = new TcpSocket();
 soc.bind(new InternetAddress("localhost", 10101));
 soc.listen(10);
 Stream str = new SocketStream(soc.accept());
 while(true)
 {
 	char[] line = str.readLine();
 	if (line !is null)
 		cmdOpen(line);
 }
 
 client.d:
 Socket socket = new TcpSocket(new InternetAddress("localhost", 10101));
 Stream stream = new SocketStream(socket);
 stream.writeString(args[1]);
 socket.close();
 
 Can anyone point out what I'm doing wrong? The first connection works fine,
but any connection afterwords fails (line IS null on second connection).
 
 Thanks!
while (true) { Socket clientSocket = soc.accept(); Stream str = new SocketStream(clientSocket); while(clientSocket.isAlive()) { char[] line = str.readLine(); if (line !is null) cmdOpen(line); } }
Thanks for the proper code, however it still doesn't allow a 2nd connection, or any after that for that matter. line defines to null. Any ideas?
Jan 15 2008
parent reply BCS <BCS pathlink.com> writes:
okibi wrote:
while (true)
{
	Socket clientSocket = soc.accept();
if it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.
	Stream str = new SocketStream(clientSocket);
	while(clientSocket.isAlive())
	{
	 	char[] line = str.readLine();
If this is the loop that is not working then I don't think it is a socket issue. One thing does come to mind, I expect readLine needs to work with CR/LF sets and as I recall a number of cases (telnet and such) don't work exactly as I would expect WRT this. Try testing this inner loop with a file or STDIN.
	 	if (line !is null)
	 		cmdOpen(line);
	}
}
Thanks for the proper code, however it still doesn't allow a 2nd connection, or any after that for that matter. line defines to null. Any ideas?
Jan 15 2008
next sibling parent okibi <okibi ratedo.com> writes:
BCS Wrote:

 okibi wrote:
while (true)
{
	Socket clientSocket = soc.accept();
if it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.
	Stream str = new SocketStream(clientSocket);
	while(clientSocket.isAlive())
	{
	 	char[] line = str.readLine();
If this is the loop that is not working then I don't think it is a socket issue. One thing does come to mind, I expect readLine needs to work with CR/LF sets and as I recall a number of cases (telnet and such) don't work exactly as I would expect WRT this. Try testing this inner loop with a file or STDIN.
	 	if (line !is null)
	 		cmdOpen(line);
	}
}
Thanks for the proper code, however it still doesn't allow a 2nd connection, or any after that for that matter. line defines to null. Any ideas?
It didn't even occur to me that readLine is waiting for a line break! I'll give it a try and report back. (this might be a duh! moment) Thanks!
Jan 15 2008
prev sibling parent reply James Dennett <jdennett acm.org> writes:
BCS wrote:
 okibi wrote:
 while (true)
 {
     Socket clientSocket = soc.accept();
if it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.
Maybe it's worth noting that it takes only one connection at a time unlike most real servers which handle concurrent connections. -- James
Jan 15 2008
parent reply okibi <okibi ratedo.com> writes:
James Dennett Wrote:

 BCS wrote:
 okibi wrote:
 while (true)
 {
     Socket clientSocket = soc.accept();
if it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.
Maybe it's worth noting that it takes only one connection at a time unlike most real servers which handle concurrent connections. -- James
Client side will make a connection, send a string, and disconnect prior to another connection being made. Anyways, the line break idea didn't work.
Jan 15 2008
parent reply okibi <okibi ratedo.com> writes:
okibi Wrote:

 James Dennett Wrote:
 
 BCS wrote:
 okibi wrote:
 while (true)
 {
     Socket clientSocket = soc.accept();
if it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.
Maybe it's worth noting that it takes only one connection at a time unlike most real servers which handle concurrent connections. -- James
Client side will make a connection, send a string, and disconnect prior to another connection being made. Anyways, the line break idea didn't work.
By throwing in some writef's, I've been able to get multiple connections. However, the second connection fails upon completion. I'm getting errors with "shouldn't be able to reach 'insert memory location'" as well as "cannot reference 'insert memory location'" upon handling the data passed. Odd...
Jan 16 2008
parent reply BCS <BCS pathlink.com> writes:
okibi wrote:

 By throwing in some writef's, I've been able to get multiple connections.
 
 I'm getting errors with "shouldn't be able to reach 'insert memory location'"
If you do this int main() { while(true) {...} } and the loop some how ends (break, throw, etc.) then you get some strange stuff happening when execution gets to the end of the main without a return. Make it "void main()" and your fine. Just a though, don't know if it is what's happening.
 as well as "cannot reference 'insert memory location'" upon handling 
the data passed. Odd...
Jan 16 2008
parent okibi <okibi ratedo.com> writes:
BCS Wrote:

 okibi wrote:
 
 By throwing in some writef's, I've been able to get multiple connections.
 
 I'm getting errors with "shouldn't be able to reach 'insert memory location'"
If you do this int main() { while(true) {...} } and the loop some how ends (break, throw, etc.) then you get some strange stuff happening when execution gets to the end of the main without a return. Make it "void main()" and your fine. Just a though, don't know if it is what's happening. > as well as "cannot reference 'insert memory location'" upon handling the data passed. Odd...
My main() functions are "void". The only one that is "int" is run() within the thread that contains the server-side connection.
Jan 17 2008