digitalmars.D.learn - Sockets issue
- okibi (19/19) Jan 15 2008 I'm a bit puzzled here. I'm trying to get my sockets working, but it onl...
- Raynor (12/35) Jan 15 2008 while (true)
- okibi (3/39) Jan 15 2008 Thanks for the proper code, however it still doesn't allow a 2nd connect...
- BCS (8/24) Jan 15 2008 if it runs to here then you have accepted a connection, if it gets here
- okibi (4/32) Jan 15 2008 It didn't even occur to me that readLine is waiting for a line break! I'...
- James Dennett (5/12) Jan 15 2008 Maybe it's worth noting that it takes only one connection
- okibi (3/18) Jan 15 2008 Client side will make a connection, send a string, and disconnect prior ...
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
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
Raynor Wrote:okibi a écrit :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?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
okibi wrote:if it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.while (true) { Socket clientSocket = soc.accept();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.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
BCS Wrote:okibi wrote: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!if it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.while (true) { Socket clientSocket = soc.accept();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.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
BCS wrote:okibi wrote:Maybe it's worth noting that it takes only one connection at a time unlike most real servers which handle concurrent connections. -- Jamesif it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.while (true) { Socket clientSocket = soc.accept();
Jan 15 2008
James Dennett Wrote:BCS wrote: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.okibi wrote:Maybe it's worth noting that it takes only one connection at a time unlike most real servers which handle concurrent connections. -- Jamesif it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.while (true) { Socket clientSocket = soc.accept();
Jan 15 2008
okibi Wrote:James Dennett Wrote: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...BCS wrote: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.okibi wrote:Maybe it's worth noting that it takes only one connection at a time unlike most real servers which handle concurrent connections. -- Jamesif it runs to here then you have accepted a connection, if it gets here a second time, you have accepted a second connection.while (true) { Socket clientSocket = soc.accept();
Jan 16 2008
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 handlingthe data passed. Odd...
Jan 16 2008
BCS Wrote:okibi wrote:My main() functions are "void". The only one that is "int" is run() within the thread that contains the server-side connection.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 17 2008