www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Sockets and Streams

reply okibi <spam ratedo.com> writes:
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!
Apr 04 2008
parent reply okibi <spam ratedo.com> writes:
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.
Apr 04 2008
parent reply Tobias Kieslich <tobias justdreams.de> writes:
Hi, I think people need a bit more information, the code looks okay to
me, but what is srvCMD doing? Is that crashing? Does it have a return
value? Did you try wrapping it into a try, catch statement? My mony is on
this function.

	-T
On Fri, 04 Apr 2008, okibi wrote:

 okibi Wrote:
 
 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();
 	}
 }
 
 Sorry, the first clientSocket.close() shouldn't be there in the server code.
Apr 04 2008
parent reply okibi <spam ratedo.com> writes:
Tobias Kieslich Wrote:

 Hi, I think people need a bit more information, the code looks okay to
 me, but what is srvCMD doing? Is that crashing? Does it have a return
 value? Did you try wrapping it into a try, catch statement? My mony is on
 this function.
 
 	-T
 On Fri, 04 Apr 2008, okibi wrote:
 
 okibi Wrote:
 
 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();
 	}
 }
 
 Sorry, the first clientSocket.close() shouldn't be there in the server code.
It's not that function. That function is used by the server and works just fine. However, I want clients to be able to connect and send a param to that function. The problem is that once the client connects and calls the function, it varies how much of the function will run prior to it crashing. For example, in the function I'm doing three things: writing to a file, adding to a window on the server, and then changing some other data. It varies how far through this process it can get and I don't know why. Sometimes (but only on the first connect) it will do the whole process fine. Is the client disconnecting too soon? Is the server not listening correctly? I just don't see what's wrong! What all would you need to see?
Apr 06 2008
parent reply Tobias Kieslich <tobias justdreams.de> writes:
On Sun, 06 Apr 2008, okibi wrote:

 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();
 		if(line !is null)
 		{
 			srvCMD(replace(line, "\n", ""));
 			line = null;
 		}
 		clientSocket.close();
 	}
 }
 
 It's not that function. That function is used by the server and works just
fine. However, I want clients to be able to connect and send a param to that
function.
 
 The problem is that once the client connects and calls the function, it varies
how much of the function will run prior to it crashing. For example, in the
function I'm doing three things: writing to a file, adding to a window on the
server, and then changing some other data. It varies how far through this
process it can get and I don't know why. Sometimes (but only on the first
connect) it will do the whole process fine.
 
 Is the client disconnecting too soon? Is the server not listening correctly? I
just don't see what's wrong!
okay, I'm getting confused here so I wanna make sure I get it right: if the client connects, and sends one command the srvCMD function triggers more than one action, or is each actiontriggred by a different command? In the latter case I probably would use a select() based server that monitors the socket. -Tobias
Apr 07 2008
parent okibi <spam ratedo.com> writes:
Tobias Kieslich Wrote:

 On Sun, 06 Apr 2008, okibi wrote:
 
 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();
 		if(line !is null)
 		{
 			srvCMD(replace(line, "\n", ""));
 			line = null;
 		}
 		clientSocket.close();
 	}
 }
 
 It's not that function. That function is used by the server and works just
fine. However, I want clients to be able to connect and send a param to that
function.
 
 The problem is that once the client connects and calls the function, it varies
how much of the function will run prior to it crashing. For example, in the
function I'm doing three things: writing to a file, adding to a window on the
server, and then changing some other data. It varies how far through this
process it can get and I don't know why. Sometimes (but only on the first
connect) it will do the whole process fine.
 
 Is the client disconnecting too soon? Is the server not listening correctly? I
just don't see what's wrong!
okay, I'm getting confused here so I wanna make sure I get it right: if the client connects, and sends one command the srvCMD function triggers more than one action, or is each actiontriggred by a different command? In the latter case I probably would use a select() based server that monitors the socket. -Tobias
It triggers one command, but the server is required to do many things with that one command. I just need the server to receive a string from any given client and be able to do a few things with it. The client doesn't need to remain connected.
Apr 08 2008