Sockets and Streams

okibi spam at ratedo.com
Fri Apr 4 03:38:55 PDT 2008


okibi Wrote:

> I've asked something similar before, but my problem doesn't seem to be resolved.
> 
> Anyways, I have a program that I'm trying to use sockets to get it to talk to other instances. I have a server portion set up, as well as the client side, which seems to run pretty good. I'm using tcpsockets and socketstreams, and just doing a listn and wait for accept on the server side.
> 
> My question is this: Even though it usually connects just fine, once I verify that the connection is made and try to hit a function on the server side via the server portion, it will either run part of it, or none of it and crash. Am I maybe doing it wrong? This is my current code:
> 
> server:
> auto soc = new TcpSocket();
> soc.bind(new InternetAddress("localhost", 10101));
> soc.listen(10);
> while (true)
> {
> 	Socket clientSocket = soc.accept();
> 	Stream str = new SocketStream(clientSocket);
> 	if(clientSocket.isAlive())
> 	{
> 		char[] line = str.readLine();
> 		clientSocket.close();
> 		if(line !is null)
> 		{
> 			srvCMD(replace(line, "\n", ""));
> 			line = null;
> 		}
> 		clientSocket.close();
> 	}
> }
> 
> client:
> Socket socket = new TcpSocket(new InternetAddress("localhost", 10101));
> Stream stream = new SocketStream(socket);
> if(socket.isAlive())
> {
> 	stream.writeString(args[1]~"\n");
> 	socket.close();;
> }
> 
> For the client, args[1] is simply the argument passed to the client that I'm wanting to relay to the server to get processed.
> 
> Am I doing something wrong?
> 
> Thanks!

Sorry, the first clientSocket.close() shouldn't be there in the server code.


More information about the Digitalmars-d-learn mailing list