Sockets issue

okibi okibi at ratedo.com
Tue Jan 15 04:50:43 PST 2008


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?


More information about the Digitalmars-d-learn mailing list