sacridex
May 31st, 2012, 11:12 AM
Hello,
I am trying to write a simple server and client application. The Client connects to the server, sends a message and the server shall respond.
But there is a problem: The Server receives the message from the client(and writes it into the stdout), but the responding message is not sent at all, the client is waiting forever. When I quit the client(ctrl+c), the message is sent(obv to late).
Here is my code - Server:
void process_request (InputStream input, OutputStream output) throws Error
{
var data_in = new DataInputStream (input);
string line;
while ((line = data_in.read_line (null)) != null)
{
stdout.printf ("%s\n", line);
}
data_in.close ();
string content = "Hello!";
stdout.printf ("sent?: %lu\n", output.write (content.data));
output.flush ();
}
int main ()
{
try
{
var service = new SocketService ();
service.add_inet_port (1337, null);
service.start ();
while (true)
{
var conn = service.accept (null);
process_request (conn.input_stream, conn.output_stream);
}
}
catch (Error e)
{
stderr.printf ("%s\n", e.message);
}
return 0;
}Client:
class Client : Object
{
public Client ()
{
var add = new InetAddress.from_string ("127.0.0.1");
try
{
var sock = new SocketClient ();
var conn = sock.connect (new InetSocketAddress (add, 1337));
var output = conn.output_stream;
output.write ("test\n".data);
output.flush ();
var response = new DataInputStream (conn.input_stream);
var line = response.read_line (null);
stdout.printf ("received message: %s\n", line);
}
catch (Error e)
{
stdout.printf ("error: " + e.message + "\n");
}
}
public static int main ()
{
new Client ();
return 0;
}
}
I am trying to write a simple server and client application. The Client connects to the server, sends a message and the server shall respond.
But there is a problem: The Server receives the message from the client(and writes it into the stdout), but the responding message is not sent at all, the client is waiting forever. When I quit the client(ctrl+c), the message is sent(obv to late).
Here is my code - Server:
void process_request (InputStream input, OutputStream output) throws Error
{
var data_in = new DataInputStream (input);
string line;
while ((line = data_in.read_line (null)) != null)
{
stdout.printf ("%s\n", line);
}
data_in.close ();
string content = "Hello!";
stdout.printf ("sent?: %lu\n", output.write (content.data));
output.flush ();
}
int main ()
{
try
{
var service = new SocketService ();
service.add_inet_port (1337, null);
service.start ();
while (true)
{
var conn = service.accept (null);
process_request (conn.input_stream, conn.output_stream);
}
}
catch (Error e)
{
stderr.printf ("%s\n", e.message);
}
return 0;
}Client:
class Client : Object
{
public Client ()
{
var add = new InetAddress.from_string ("127.0.0.1");
try
{
var sock = new SocketClient ();
var conn = sock.connect (new InetSocketAddress (add, 1337));
var output = conn.output_stream;
output.write ("test\n".data);
output.flush ();
var response = new DataInputStream (conn.input_stream);
var line = response.read_line (null);
stdout.printf ("received message: %s\n", line);
}
catch (Error e)
{
stdout.printf ("error: " + e.message + "\n");
}
}
public static int main ()
{
new Client ();
return 0;
}
}