Thread overview |
---|
January 15, 2008 Sockets issue | ||||
---|---|---|---|---|
| ||||
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! |
January 15, 2008 Re: Sockets issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | 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);
}
}
|
January 15, 2008 Re: Sockets issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to Raynor | 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?
|
January 15, 2008 Re: Sockets issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | 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? |
January 15, 2008 Re: Sockets issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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!
|
January 15, 2008 Re: Sockets issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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
|
January 15, 2008 Re: Sockets issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dennett | 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.
|
January 16, 2008 Re: Sockets issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | 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...
|
January 16, 2008 Re: Sockets issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to okibi | 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...
|
January 17, 2008 Re: Sockets issue | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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.
|
Copyright © 1999-2021 by the D Language Foundation