Hello everybody,
I have a question about communicating with a server on c# and a simulator in jmonkey using websockets.
I’m fairly new to websockets and that’s probably why I’m stuck at this moment.
In C# I have a little connection set up like this:
[java]
server = new TcpListener(IPAddress.Parse(“127.0.0.1”), port);
server.Start();
Console.WriteLine("Server has started on 127.0.0.1:"+ port +".{0}Waiting for a connection...", Environment.NewLine);
client = server.AcceptTcpClient();
Console.WriteLine("A client connected.");
NetworkStream stream = client.GetStream();
while(true)
{
//while (!stream.DataAvailable) ;
if(stream.DataAvailable)
Console.WriteLine(stream.Read(incoming,0,incoming.Length));
if(client.Connected)
stream.Write(outgoing,0,outgoing.Length);
System.Threading.Thread.Sleep(500);
}
[/java]
I think nothing is wrong with this code, correct me if I’m wrong.
Next I connect using the following code:
[java]package mygame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
/**
*
-
@author hreitsma
*/
public class Connection implements Runnable {
private int netPort = 1337;
private Socket socket;
private PrintWriter out;
private BufferedReader in;
private Intersection intersection;
public Connection(Intersection _intersection)
{
this.intersection = _intersection;
}
@Override
public void run()
{
connectToServer();
}
public synchronized void readCommand(String input)
{
intersection.getRoad(0).setTrafficLight(0);
}
private void connectToServer()
{
closeSocket();
waitForServer();
try {
System.out.println("start reading");
while(true)
{
System.out.println(in.read()); // this works like a charm
out.write(11111); // this gets lost or something like that :/
}
// while ((line = in.readLine()) != null) {
// readCommand(line);
// }
} catch (SocketException ex) {
System.out.println(“Server has disconnected. Waiting for new server.”);
connectToServer();
} catch (IOException e) {
System.out.println("Cannot read line");
connectToServer();
}
}
public void waitForServer()
{
System.out.print("Waiting for server.");
while (true) {
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), netPort);
//socket = new Socket(InetAddress.getByName("192.168.2.2"), netPort);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Connected to server!");
break;
} catch (Exception e) {
System.out.print(".");
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void closeSocket()
{
if (socket != null) {
if (!socket.isClosed()) {
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
[/java]
When I start both programs the following happens:
server (C#):
Server has started on 127.0.0.1:1337
Waiting for a connection…
A client connected
client (JMonkey):
Waiting for server.
Connected to server!
start reading
0
1
0
1
1
etc…
JMonkey receives data perfectly, but C# doesn’t get data at all or JMonkey doesn’t send anything at all.
Can somebody explain what I’m totally doing wrong here?
Thanks a lot in advance!