I am trying to set up a basic server client pair and am able to connect the client to the server, but when the client uses transmission control protocol to send an object it is never recieved.
Here is the server :
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.minlog.Log;
public class ChatServer {
PrintWriter log;
public static void main(String[] args) {
Server server = new Server() {
protected Connection newConnection () {
//System.out.println(" Server : Client Connected");
PrintWriter writer;
try {
writer = new PrintWriter("ClientConnected.txt", "UTF-8");
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new ChatConnection();
}
};
Kryo k = server.getKryo();
k.register(Request.class);
server.addListener(new Listener() {
public void received (Connection c, Object object) {
PrintWriter writer;
try {
writer = new PrintWriter("RecievedAMessage.txt", "UTF-8");
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
/*
if (object instanceof Request) {
Request request = (Request)object;
System.err.println(request.text);
Response response = new Response();
response.text = "Thanks";
connection.sendTCP(response);
}
*/
}
});
try {
server.bind(6661, 6662);
} catch (IOException e) {
System.err.println("Error : Couldnt establish server on requested port(s)");
System.exit(1);
}
server.start();
}
static class ChatConnection extends Connection {
public String name;
}
}
And the client :
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Listener;
public class ChatClient {
static Client client;
public static void main(String[] args) {
client = new Client();
client.start();
Kryo k = client.getKryo();
k.register(Request.class);
System.out.println("Client has started");
try {
client.connect(5000, "Mufasa", 6661);
System.out.println("Client has connected");
} catch (IOException e) {
System.err.println("Unable to conneI ct");
System.exit(1);
}
JFrame frame = new JFrame();
frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton sendButton = new JButton("Send");
sendButton.setSize(100,50);
sendButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Sending...");
Request request = new Request();
request.text = "Here is the request";
client.sendTCP(request);
System.out.println("Sent");
}
});
frame.add(sendButton);
System.out.println("GUI Loaded");
}
}
The server should create a blank text file called Recieved.txt when it recieves anything. It does create a blank text file called Connected.txt when the client connects.
Also it is just a LAN server client pair, my computer name is Mufasa so that is the in place of the IP adress.
Thanks in advance.