Let me jump right in. Here's my Server class:
public class DTServer {
ServerSocket serverSocket;
ServerSocketHints serverSocketHints;
Socket socket;
InputStream inputStream;
OutputStream outputStream;
ObjectInputStream objectInputStream;
ObjectOutputStream objectOutputStream;
public DTServer(int port) {
serverSocketHints = new ServerSocketHints();
serverSocketHints.acceptTimeout = 0;
serverSocket = Gdx.net.newServerSocket(
Net.Protocol.TCP, port, serverSocketHints);
socket = serverSocket.accept(null);
}
public Serialized receiveSerialized() {
inputStream = socket.getInputStream();
try {
objectInputStream = new ObjectInputStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
outputStream = socket.getOutputStream();
try {
objectOutputStream = new ObjectOutputStream(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
Serialized serialized = new Serialized();
try {
serialized = (Serialized) objectInputStream.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return serialized;
}
}
And the Client class:
public class DTClient {
Socket socket;
SocketHints socketHints;
InputStream inputStream;
OutputStream outputStream;
ObjectInputStream objectInputStream;
ObjectOutputStream objectOutputStream;
public DTClient(String address, int port) {
socketHints = new SocketHints();
socketHints.connectTimeout = 3000;
socketHints.keepAlive = true;
// socketHints.trafficClass = 0x04; //IPTOS_RELIABILITY
socket = Gdx.net.newClientSocket(
Net.Protocol.TCP, address, port, socketHints);
}
public void sendSerialized(Serialized serialized) {
inputStream = socket.getInputStream();
try {
objectInputStream = new ObjectInputStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
outputStream = socket.getOutputStream();
try {
objectOutputStream = new ObjectOutputStream(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
objectOutputStream.writeObject(serialized);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I create the server object like this:
public void startServer() {
new Thread(new Runnable() {
#Override
public void run() {
dtServer = new DTServer(32658);
System.out.println("Server started and listening at port: 32658.");
}
}).start();
}
And then connect to it with other instance of the program:
public void connect() {
new Thread(new Runnable() {
#Override
public void run() {
dtClient = new DTClient("127.0.0.1", 32658);
System.out.println("Connected to server at 127.0.0.1:32658");
}
}).start();
}
And everything work swell until I try to receive the object I sent:
game.dtClient.sendSerialized(new Serialized(game.gameScreen.localPlayer));
//client side
Serialized s = (Serialized)game.dtServer.receiveSerialized();
//server side
Calling receiveSerialized method causes both app instances to freeze.
Create the ObjectOutputStream before the ObjectInputStream, at both ends. Otherwise you can get a deadlock trying to read the object stream header.
You should also use the same object streams for the life of the socket, rather than a new pair per message.
Related
I am trying to send a .mkv file with a Java server socket. The file is beeing transmitted normally but when I try to open it with VLC Media Player it only shows the first second of the film and then the pircture freezes.
Server:
public class Server {
public void run_server(){
try {
ExecutorService executorService = Executors.newFixedThreadPool(100);
ServerSocket server = new ServerSocket(1234);
System.out.println("Waiting for client at port " + server.getLocalPort() + "\n");
while (true) {
Socket client = server.accept();
executorService.execute(new Handler(client));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Server s = new Server();
s.run_server();
}
public class Handler implements Runnable {
private Socket client;
public Handler(Socket client) {
this.client = client;
}
#Override
public void run() {
try {
System.out.println("client connected: " + client.getInetAddress());
FileInputStream fin = new FileInputStream("D:\\Filme\\Asterix und das Geheimnis des Zaubertranks (2018).mkv");
byte[] buffer = new byte[522231808];
fin.read(buffer,0,buffer.length);
DataOutputStream out = new DataOutputStream(client.getOutputStream());
out.write(buffer,0,buffer.length);
} catch (Exception e) {
e.printStackTrace();
}
}
The client:
public static void main(String[] args) {
new Thread(new Client()).start();
}
#Override
public void run() {
try {
Socket client = new Socket("localhost", 1234);
DataInputStream in = new DataInputStream(client.getInputStream());
FileOutputStream fout = new FileOutputStream("D:\\test\\test.mkv");
byte[] buffer = new byte[522231808];
in.read(buffer,0,buffer.length);
fout.write(buffer,0,buffer.length);
} catch (IOException e) {
e.printStackTrace();
}
}
Where is the problem?
Thanks in advance
Try a smaller buffer:
byte[] buffer = new byte[16384];
for (int n = in.read(buffer); n > 0; n = in.read(buffer)) {
fout.write(buffer,0,n);
}
It's my first time working with sockets, in order to get a better understanding of what's going on I decided to build a client server chat application which can support several users.
At first, I used DataInputStream / DataOutputStream to communicate and everything works well. But I would like to switch to an ObjectStream and that's where the problem occurs. Once I replace all the DataInputStream / DataOutputStream by ObjectInputStream / ObjectOutputStream, I'm no longer able to print the retrieved data.
This is the code that I used before, which works (DataStream) :
SERVER:
try {
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("HI FROM SERVER");
while (!socket.isClosed()) {
try {
if (in.available() > 0) {
String input = in.readUTF();
for (ClientThread thatClient : server.getClients()){
DataOutputStream outputParticularClient = new DataOutputStream(thatClient.getSocket().getOutputStream());
outputParticularClient.writeUTF(input + " GOT FROM SERVER");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
CLIENT:
try {
socket = new Socket("localhost", portNumber);
DataInputStream in = new DataInputStream(socket.getInputStream());
new Thread(()->{
while(!socket.isClosed()){
try {
if (in.available() > 0){
String input = in.readUTF();
System.out.println(getUserName() + " > " + input);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
And this is how I tried to perform the same idea with ObjectStream :
SERVER:
try {
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
while (!socket.isClosed()) {
try {
if (in.available() > 0) {
Message input;
try {
input = (Message)in.readObject();
if (input.equals(null)){
System.err.println("SERVER RETRIEVED NULL OBJECT");
}
for (ClientThread thatClient : server.getClients()){
ObjectOutputStream outputParticularClient = new ObjectOutputStream(thatClient.getSocket().getOutputStream());
outputParticularClient.writeObject(input);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
CLIENT:
try {
socket = new Socket(getHost(), portNumber);
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
new Thread(()->{
while(!socket.isClosed()){
try {
if (in.available() > 0){
Message input = null;
try {
input = (Message)in.readObject();
if (input.equals(null)){
System.err.println("CLIENT RETRIEVED NULL OBJECT");
}
System.out.println("CLIENT " + input.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
I feel like it has something to do with this if statement if (in.available() > 0) but I cannot say precisely what's going on.
available() doesn't do what you may think it does and it is almost never useful in production code (and that's particularly true for ObjectInputStream). The reason you don't receive any data is in fact that in.available() always returns 0 as you already suspected.
As noted in the comments, the StreamCorruptedException is caused by writing to an existing ObjectInputStream that has already been written to using another instance of ObjectOutputStream. Cf. the answer StreamCorruptedException: invalid type code: AC for further explanation.
Here is some quick & dirty example code that has a server echoing the messages from two clients. It's not clean but it may give you an idea how to approach your problem:
public class SO56493162 {
private static final class Message implements Serializable {
private static final long serialVersionUID = 1L;
private static int cnt = 0;
private final int id;
public Message(int id) {
++cnt;
this.id = id;
}
public String toString() {
return "Msg from " + id + " : " + cnt;
}
}
private static final class Client implements Runnable {
private InetSocketAddress addr = null;
private int id = -1;
Client(InetSocketAddress addr, int id) {
this.addr = addr;
this.id = id;
}
public void run() {
int timeout = 3000;
Socket s = null;
try {
s = new Socket();
s.connect(addr, timeout);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
System.out.println("Client " + id + " connected");
while (true) {
Thread.sleep(new Random().nextInt(2000));
Message hello = new Message(id);
oos.writeObject(hello);
oos.flush();
Message reply = (Message) ois.readObject();
System.out.println("Reply: " + reply.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
s.close();
} catch (Exception ignore) {
}
}
}
}
private static final class Server implements Runnable {
private ServerSocket sock = null;
Server(ServerSocket sock) throws IOException {
this.sock = sock;
}
public void run() {
System.out.println("starting server");
try {
while (true) {
final Socket client = sock.accept();
System.out.println("connection accepted");
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
while (!client.isClosed()) {
try {
Message input = (Message) ois.readObject();
oos.writeObject(input);
oos.flush();
} catch (EOFException eof) {
System.err.println("EOF!");
client.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.setDaemon(true);
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws IOException, InterruptedException {
final int port = 9876;
Thread ts = new Thread(new Runnable() {
#Override
public void run() {
try {
new Server(new ServerSocket(port)).run();
} catch (Exception e) {
e.printStackTrace();
}
}
});
ts.setDaemon(true);
ts.start();
InetSocketAddress addr = new InetSocketAddress("localhost", port);
for (int i = 0; i < 2; ++i) {
Client cl = new Client(addr, i);
Thread tc = new Thread(cl);
tc.setDaemon(true);
tc.start();
}
Thread.sleep(10000);
System.err.println("done");
}
}
I am working on Socket programming. I have build such a server which should accept multiple Clients. Here I have particular num of clients , clients keeps on sending msg to Server every 10sec , Server has to process it.The problem I am having is I am unable to connect multiple Server and here a single client is a continuous running programm in while(true) So if one client Sends a request another client can not connect . Here is my Program.
Server
public class SimpleServer extends Thread {
private ServerSocket serverSocket = null;
private Socket s1 = null;
SimpleServer() {
try {
serverSocket = new ServerSocket(1231);
this.start();
} catch (IOException ex) {
System.out.println("Exception on new ServerSocket: " + ex);
}
}
public void run() {
while (true) {
try {
System.out.println("Waiting for connect to client");
s1 = serverSocket.accept();
System.out.println("Connection received from " + s1.getInetAddress().getHostName());
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = dis.readUTF();
System.out.println(st);
s1In.close();
dis.close();
s1.close();
// throw new ArithmeticException();
} catch (IOException ex) {
Logger.getLogger(SimpleServer.class.getName()).log(Level.SEVERE, null, ex);
}
catch (Exception e) {
System.out.println("Exceptiopn: "+e);
}
}
}
public static void main(String args[]) throws IOException {
new SimpleServer();
}
}
Server is working fine but I am not able to write Client program which shoud run in while(true) loop for sending msg to Server and allow other client also connect to Server.
but for a single client I write like this ,
public class SimClient extends Thread {
private Socket s1 = null;
SimClient() {
//this.start();
}
public void run() {
int i=0;
try {
s1 = new Socket("localhost", 1231);
} catch (IOException ex) {
Logger.getLogger(SimClient.class.getName()).log(Level.SEVERE, null, ex);
}
// while (i<10) {
try {
// Open your connection to a server, at port dfg1231
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(s1out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Data from Client:");
String s = br.readLine();
dos.writeUTF(s);
dos.flush();
s1out.close();
dos.close();
// s1.close();
i++;
} catch (IOException ex) {
//ex.printStackTrace();
System.out.println("Exception in While: "+ex.getMessage());
}
//}
}
public static void main(String args[]) throws IOException {
SimClient s= new SimClient();
s.start();
}
}
So can any one help me to write client program. its a great help for me.
just as you have a Thread for the ServerSocket, you need to create a Thread for every Socket returned by serverSocket.accept() , then it loops back around immediately to block and wait to accept another Socket. Make a class called SocketHander which extends Thread and accepts a Socket in the constructor.
public class SocketHandler extends Thread {
private Socket socket;
public SocketHandler(Socket socket) {
this.socket = socket;
}
public void run() {
// use the socket here
}
}
and back in the ServerSocket handler...
for (;;) {
SocketHandler socketHander = new SocketHandler(serverSocket.accept());
socketHander.start();
}
It is generally a good idea to use a Fixed Size Thread Pool because creating Threads in a ad-hoc manner may cause the Server to run out of Threads if the requests are high.
public class SimpleServer extends Thread {
private ServerSocket serverSocket = null;
private static ExecutorService executor = Executors.newFixedThreadPool(100);
SimpleServer() {
try {
serverSocket = new ServerSocket(1231);
this.start();
} catch (IOException ex) {
System.out.println("Exception on new ServerSocket: " + ex);
}
}
public void run() {
while (true) {
try {
System.out.println("Waiting for connect to client");
final Socket s1 = serverSocket.accept();
executor.execute(new Runnable() {
public void run() {
try {
System.out.println("Connection received from " + s1.getInetAddress().getHostName());
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = dis.readUTF();
System.out.println(st);
s1In.close();
dis.close();
s1.close();
} catch(Exception e) {
System.out.println("Exceptiopn: "+e);
}
// throw new ArithmeticException();
}});
} catch (IOException ex) {
Logger.getLogger(SimpleServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception e) {
System.out.println("Exceptiopn: "+e);
}
}
}
public static void main(String args[]) throws IOException {
new SimpleServer();
}
}
I am new to google protocol buffer . I am writing a client server application where client send request object to server and server return response. Currently when i send object to server neither the server respond nor throw any exception. Probably it stuck on line
Request request = Request.parseFrom(bytes);
where Request and Response are my message classes generated by protocol buffer.
My code samples are as follows
public class TCPServer {
final static Logger logger = Logger.getLogger(TCPServer.class.getName());
static int PORT = 6789;
public static void main(String argv[]) throws Exception
{
ServerSocket socket = new ServerSocket(PORT);
Socket connectionSocket = null;
while(true)
{
try{
connectionSocket = socket.accept();
}catch (IOException e) {
System.out.println("Could not listen on port:" + PORT);
System.exit(-1);
}
Thread thread = new Thread(new ServerConnection(connectionSocket));
thread.start();
}
}
}
public class ServerConnection implements Runnable{
static final Logger logger = Logger.getLogger(ServerConnection.class.getName());
String clientInput;
String serverOutput = null;
Socket connectionSocket = null;
ServerConnection(Socket connectionSocket){
this.connectionSocket = connectionSocket;
}
public void run() {
try {
InputStream input = connectionSocket.getInputStream();
ObjectInputStream inFromClient = new ObjectInputStream(input);
ObjectOutputStream outToClient = new ObjectOutputStream(connectionSocket.getOutputStream());
serveRequest(inFromClient , outToClient);
outToClient.flush();
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
System.out.println("Exception occured in ServerConnection run() method");
}
}
public void serveRequest(InputStream inFromClient, OutputStream outToClient){
try {
System.out.println("Recieving data from client");
ResponseReciever response = new ResponseReciever();
ObjectInputStream input = (ObjectInputStream) inFromClient;
byte size = input.readByte();
byte []bytes = new byte[size];
input.readFully(bytes);
Request request = Request.parseFrom(bytes);
System.out.println("Request recieved");
response.createResponse(request.getId(),request.getMessage(),true).writeTo(outToClient);
System.out.println("Response send");
} catch (Exception ex) {
logger.log(Level.SEVERE, null, ex);
System.out.println("Exception occured in ServerConnection serverRequest() method");
}
}
And my client look like this
public class TCPClient {
final static Logger logger = Logger.getLogger(TCPClient.class.getName());
private static int PORT = 6789;
private static String HOST_NAME = "localhost";
private static boolean isOpen = true;
private Socket openConnection(final String hostName,final int port){
Socket clientSocket = null;
try {
clientSocket = new Socket(HOST_NAME, PORT);
} catch (IOException e) {
logger.log(Level.SEVERE, "Exception occured while connecting to server", e);
}
return clientSocket;
}
private void closeConnection(Socket clientSocket){
try {
logger.log(Level.INFO, "Closing the connection");
clientSocket.close();
isOpen = false;
} catch (IOException e) {
logger.log(Level.SEVERE, "Exception occured while closing the connection", e);
}
}
public void sendToServer(OutputStream output){
try {
System.out.println("Sending data to server");
RequestSender requestSender = new RequestSender();
Request request = requestSender.getRequest(1,"param1","param2",23L,"Its message",true);
ObjectOutputStream outputStream = (ObjectOutputStream)output;
request.writeTo(outputStream);
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
}
}
public void recieveFromServer(InputStream input){
try {
System.out.println("Recieving data from server");
Response response = Response.parseFrom(input);
System.out.println(response.getId());
System.out.println(response.getResponse());
System.out.println(response.getError());
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
}
}
public static void main(String argv[]) throws Exception
{
ObjectOutputStream outToServer = null;
InputStream inFromServer = null;
TCPClient client = new TCPClient();
try {
while(isOpen)
{
Socket clientSocket = client.openConnection(HOST_NAME, PORT);
outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
inFromServer = new ObjectInputStream(clientSocket.getInputStream());
client.sendToServer(outToServer);
client.recieveFromServer(inFromServer);
}
}catch (Exception e) {
logger.log(Level.SEVERE, "Exception occured ", e);
System.out.println("Exception occured in TCPClient main() method");
System.exit(1);
}
}
}
I am unable to find what is wrong in the code. Please let me know if you find something missing.
It works by using writeDelimtedTo(outputStream) and parseDelimitedFrom(inputStream) instead of writeTo(outputStream) and parseFrom(inputStream). So by putting the following code on server and client sides the program works.
Server side:
InputStream input = connectionSocket.getInputStream();
OutputStream output = connectionSocket.getOutputStream();
Request request = null;
while ((request = Request.parseDelimitedFrom(input)) != null) {
System.out.println(request.toString());
}
Client side:
Socket clientSocket = client.openConnection(HOST_NAME, PORT);
Request request = getRequest();
OutputStream output = clientSocket.getOutputStream();
InputStream input = clientSocket.getInputStream();
request.writeDelimitedTo(output);
If you start sending protocol buffers over the wire - then you will need to "frame" them. The problem is reported and solved with this question: does protobuf need a network packet header?
Instead of writing all this code, you could checkout https://code.google.com/p/protobuf-rpc-pro/ and see if it satisfies your requirements for RPC between java server and java clients.
Ok so , i have a thread class called 'Client' every time the server accepts a connection it creates a new Client....The run method listens for messages from the client and i am useing ObjectInputStream ..
do {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(socket.getInputStream());
String message = (String) in.readObject();
System.out.println(message);
}
catch (ClassNotFoundException ex) {
isConnected = false;
System.out.println("Progoramming Error");
}
catch (IOException ex) {
isConnected = false;
System.out.println("Server ShutDown");
System.exit(0);
}
} while(isConnected);
The Problem i have is that why do i have to create a new ObjectInputStream every time it loops...and if i close the input stream at the end of the loop and it loops again for another message i will get a error...Please some one help
Only create the ObjectInputStream once (outside the loop) for a client connection, then put the readObject method into the loop.
Here's a working test class:
public class TestPrg {
public static void main(String... args) throws IOException {
ServerListener server = new ServerListener();
server.start();
Socket socketToServer = new Socket("localhost", 15000);
ObjectOutputStream outStream = new ObjectOutputStream(socketToServer.getOutputStream());
for (int i=1; i<10; i++) {
try {
Thread.sleep((long) (Math.random()*3000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Sending object to server ...");
outStream.writeObject("test message #"+i);
}
System.exit(0);
}
static class ServerListener extends Thread {
private ServerSocket serverSocket;
ServerListener() throws IOException {
serverSocket = ServerSocketFactory.getDefault().createServerSocket(15000);
}
#Override
public void run() {
while (true) {
try {
final Socket socketToClient = serverSocket.accept();
ClientHandler clientHandler = new ClientHandler(socketToClient);
clientHandler.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class ClientHandler extends Thread{
private Socket socket;
ObjectInputStream inputStream;
ClientHandler(Socket socket) throws IOException {
this.socket = socket;
inputStream = new ObjectInputStream(socket.getInputStream());
}
#Override
public void run() {
while (true) {
try {
Object o = inputStream.readObject();
System.out.println("Read object: "+o);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
In this example Strings are sent trough the ObjectStream. If you get the ClassNotFoundException (http://download.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html#readObject()) and are using an independent client and server program than you might check if both the client and the server have the class of the object to send in their class paths.
The problem i personally had with Sockets and ObjectIOStream, is that it remembers all your object addresses, so each time you send and recive them on the client, if the address of sent object is not changed it will be copied from buffer. So
So
or create new objects each time you send them ( this is not a bad idiea, because ObjectIOStream seems to has limits on this buffer)
or use another Stream for these perpouse