Receive an object over TCP/IP - java

I am going to write a program over TCP/IP and I should send objects by client or by server, It is going right when I want to send or receive strings but when I am trying to read an object:
private Socket client;
public ThreadedClient(Socket client) {
this.client = client;
}
#Override
public void run() {
try {
ObjectInputStream objIn = new ObjectInputStream(client.getInputStream());
while(true){
try {
Object fromClient = objIn.readObject();
} catch (ClassNotFoundException e) {e.printStackTrace();}
}
} catch (IOException e) {e.printStackTrace();}
}
I receive an exception:
java.io.StreamCorruptedException: invalid stream header: 306E6165
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at org.bihe.serverSocket.ThreadedClient.run(Server.java:137)
at java.lang.Thread.run(Unknown Source)
and it refers to this line:
ObjectInputStream objIn = new ObjectInputStream(client.getInputStream());
It is my server code:
ServerSocket ss = new ServerSocket(8800);
while(true){
Socket newClient = ss.accept();
System.out.println(">>>> Client number " + (++counter) + " connected.");
OutputStream outputStream = newClient.getOutputStream();
PrintWriter sender = new PrintWriter(outputStream);
sender.println(true);
sender.flush();
ThreadedClient client = new ThreadedClient(newClient);
clients.add(client);
new Thread(client).start();
Client side code:
sc = new Socket("127.0.0.1", 8800);
InputStream inputStream = sc.getInputStream();
Scanner scanner = new Scanner(inputStream);
boolean s = scanner.nextBoolean();
if(s){
System.out.println("Client connected successfully.");
return true;
}else{
System.out.println("Ohhh, Some problem happened, try again later!");
}
Can anyone explain me what is happening, what is this exception and why I received this exception?

If you want to send object over network you must serialize your objects.
Check this question:
How To send an object over TCP in Java
Java Serialization:
Serialization
You could do it like this:
import java.net.*;
import java.io.*;
class testobject implements Serializable {
int value;
String id;
public testobject(int v, String s ){
this.value=v;
this.id=s;
}
}
public class SimpleServer {
public static void main(String args[]) {
int port = 2002;
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject to = (testobject)ois.readObject();
if (to!=null){System.out.println(to.id);}
System.out.println((String)ois.readObject());
is.close();
s.close();
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]){
try{
Socket s = new Socket("localhost",2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
testobject to = new testobject(1,"object from client");
oos.writeObject(to);
oos.writeObject(new String("another object from the client"));
oos.close();
os.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}

Just get rid of sending and receiving the Boolean. It's redundant. If there was some problem creating the connection, the socket wouldn't get created: an exception would be thrown instead. You're confusing everything with multiple streams on the same socket. Don't do that.
In your read-object loop, you need to catch EOFException separately, and when you get it, close the socket and exit the loop. If you get any other IOException, log it, close the socket, and exit the loop.

If you'd like to achieve good performance and send object then you definitely should use Google Protobuf
It allows you to define messages in simple .proto files. Then you use bundled compiler to generate Java classes which will be serialized and sent.
Also better idea is to use Netty over plain Java sockets. This prevent you from writing a lot of boilerplate code and define simple serialization/deserialization pipelines. Take a look at user-guide.

Related

Not getting desired output in Creating a Multiclient Chat Server?

I am trying to create a multiclient chat sort of server in which we have multiple clients connecting to server and whatever message a client enters, it gets displayed to all the clients(including the client who sent the message). I am not getting this output, instead the message just echoes only on the sender client and no other client. Code is quite long, hence i am displaying snippets of whichever code i think will help you understand error. In case, it is not enough, just comment which part you require. Thanks in advance. I am stuck on this since about hour and half, so i appreciate whatever help i would get.
The Server Class
public class Multiserver {
ServerSocket serversocket;
Socket socket;
ArrayList<Socket> al = new ArrayList<Socket>();
DataInputStream dis;
DataOutputStream dos;
Multiserver() throws IOException
{
serversocket = new ServerSocket(1036);
System.out.println("Server started on port 1036");
while(true)
{
socket = serversocket.accept();
System.out.println(socket);
al.add(socket);
Mythread thread = new Mythread(socket, al);
thread.start();
}
}
Thread used in server class
public class Mythread extends Thread{
Socket socket;
ArrayList al;
DataInputStream dis;
DataOutputStream dos;
Mythread(Socket socket, ArrayList al)
{
this.socket = socket;
this.al = al;}
public void run()
{
try{
String data ="";
dis = new DataInputStream(socket.getInputStream());
data = dis.readUTF();
if(!data.equals("stop"))
{
broadcast(data);
}
else
{
dos = new DataOutputStream(socket.getOutputStream());
// data = dos.readUTF();
dos.writeUTF(data);
dos.flush();
//dos.close();
}
}
catch(Exception e){
System.out.println("Run "+e);
}
}
public void broadcast(String data)
{
try{
Iterator it = al.iterator();
while(it.hasNext())
{
Socket socket1 = (Socket)it.next();
dos = new DataOutputStream(socket1.getOutputStream());
dos.writeUTF(data);
dos.flush();
}
}
catch(Exception e){
System.out.println("Broadcast running "+ e);
}
}
}
The client class
public class Multiclient {
Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
socket = new Socket("127.0.0.1", 1036);
System.out.println(socket);
Mythreadc my = new Mythreadc(socket);
my.start();
}
Thread used in client class
public class Mythreadc extends Thread{
DataInputStream dis;
DataOutputStream dos;
Socket socket;
Mythreadc(Socket socket)throws IOException
{
this.socket = socket;}
public void run()
{
BufferedReader br = null;
try{
br = new BufferedReader(new InputStreamReader (System.in));
dos = new DataOutputStream(socket.getOutputStream());
String data = "";
do{
data = br.readLine();
dos.writeUTF(data);
System.out.println(data);
dos.flush();
}
while(!data.equals("stop"));
}
catch(Exception e)
{
System.out.println("Client input "+e);
}
finally{
try{
br.close();
dis.close();
dos.close();
}
catch(Exception e)
{
System.out.println("Closing "+e);
}
}
}
}
I am sorry i have put on such a long code, almost all the program. But i feel it is necessary to understand where the problem lies.I have tried and i think it lies in the part where we display data written in the client's socket in the client thread class but i don't know what it is ???
#EDIT: Forgot to mention. The client stops when he sends the message "Stop"!
There are two problems with your code that are preventing the clients from displaying more than one message.
Problem one: Your client code never actually displays or prints out the messages it receives from the server. The line
dos = new DataOutputStream(socket.getOutputStream());
creates an OutputStream you can use to write data to the socket, i.e. send messages to the server. But you never use the socket's InputStream, which is what you need to do to read data from the socket, i.e. receive messages from the server. When you see a message printed out on the client, you're actually just seeing the result of
System.out.println(data);
which has your client print the message it just sent.
In order for the client to accept input from the user and read messages from the server at the same time, you should probably use two threads on the client. One thread can just be the client thread you already wrote, since it takes care of accepting input from the user. The other thread should look something like this:
public class ClientReaderThread extends Thread {
Socket socket;
ClientReaderThread(Socket socket) {
this.socket = socket;
}
public void run() {
try (BufferedReader serverReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()))){
String fromServer = serverReader.readLine();;
while(fromServer != null) {
if (fromServer.equals("stop"))
break;
System.out.println(fromServer);
fromServer = serverReader.readLine();
}
} catch (IOException e) {
System.out.println("Client error! Got exception: " + e);
}
}
}
(Note that I use the try-with-resources statement to construct the reader, which takes care of closing it when the client stops).
Then in your main client class, start both threads with the same socket:
Multiclient() throws IOException
{
socket = new Socket("127.0.0.1", 1036);
System.out.println(socket);
Mythreadc my = new Mythreadc(socket);
ClientReaderThread reader = new ClientReaderThread(socket);
my.start();
reader.start();
}
Problem two: Your server only reads and echoes a single line from each client, because the socket thread that handles each client (Mythread) doesn't contain a loop. With your setup of creating a single thread per client, run() only gets called once per client, so that run() method needs to handle every message that client sends.
Here's how the run() method in the server's thread should look:
public void run() {
try (BufferedReader inStream = new BufferedReader(
new InputStreamReader(socket.getInputStream()))){
String data = inStream.readLine();
while(data != null) {
if(data.equals("stop"))
break;
broadcast(data);
data = inStream.readLine();
}
}
catch(Exception e){
System.out.println("Run exception "+e);
} finally {
al.remove(socket); //This is important to do
}
}
I made an additional important change here: at the end of the run() method, when either the client disconnected or an exception happened, the thread removes its socket from the ArrayList. This ensures that other server threads, which all reference the same ArrayList, don't try to broadcast to the socket of a client that has disconnected. If you neglect to do this, you'll get an exception when a client sends a message to the server after another client has disconnected.
Miscellaneous notes
As I mentioned in my comment, you should give al a type of ArrayList<Socket> inside the thread class, and use a for-each loop instead of an Iterator to iterate over it in broadcast().
I'm using BufferedReader instead of DataInputStream to read from the socket. That's because DataInputStream.readUTF() and writeUTF() are deprecated, and have been replaced with BufferedReader.readLine() and PrintWriter.println().
The streams like dis and dos don't need to be instance variables in your thread classes, since they are only ever used inside the run() method. They can be local variables inside run(), like I did with inStream in my new run() method.
I think you just missed passing the ArrayList of Sockets Users Currently Connected to The Server to the thread
and Instead of Posting your Server Class You have just posted Client Program 2 times anyway ,
Your ServerClass should be build in this way : -
As soon as ServerClass recieves the request from any client, Server Class should add the Socket into ArrayList and create New Thread and just pass both to the MyThread Class
Edit :
It seems you haven't written code for Displaying the data you will get from the server .
At Client Side for Sending the Message You can simple write that in Main Thread that is under Your Client Class's Main Mehtod
You actually needed Thread at client side not for sending the message but rather for Listening the Message from the server,
because you never known when anyone can send you the message but you will always know when you want to send message to anybody connected to this chat App
Now coming to the Coding Part :
Client Class
public class Multiclient {
Socket socket;
DataInputStream dis;
DataOutputStream dos;
Multiclient() throws IOException
{
socket = new Socket("127.0.0.1", 1036);
System.out.println(socket);
Mythreadc my = new Mythreadc(socket);
my.start();
/**
* Here write out the code for taking input from Standard Console
*/
BufferedReader br = null;
try{
br = new BufferedReader(new InputStreamReader (System.in));
dos = new DataOutputStream(socket.getOutputStream());
String data = "";
do{
data = br.readLine();
dos.writeUTF(data);
System.out.println(data);
dos.flush();
}
while(!data.equals("stop"));
}
catch(Exception e)
{
System.out.println("Client input "+e);
}
}
Client Thread
try{
String data ="";
dis = new DataInputStream(socket.getInputStream());
while(data.equalsIgnorCase("stop")){
data = dis.readUTF();
System.out.println("Server Message : "+data);
}
}
catch(Exception e){
System.out.println("Run "+e);
}
Client Thread is not complete but i think this information is sufficient enough .
Hope It help you out , Your problem do remind me of College Days :)

Java server connection reset

I've been trying to fix "connection reset" problem in a simple java server-client program for a while.
My scenario is like this
client program will take filename as input, then send it to server program. Server will check if that file exists in the directory. If exist then server will print "ok", otherwise "file not found"
I'm getting this execption java.net.SocketException: Connection reset
Server program
package tcpserver;
import java.net.*;
import java.io.*;
public class TCPServer {
ServerSocket serversocket;
Socket socket;
BufferedReader buffread, buffout;
String filename;
String strDir = "D:\";
private void findFile(String name) {
File fileObj = new File(strDir);
File[] fileList = fileObj.listFiles();
if (fileList != null) {
for (File indexFile : fileList) {
if (name.equalsIgnoreCase(indexFile.getName())) {
System.out.println("200 ok ");
} else {
System.out.println("File Not found");
}
}
}
}
public TCPServer() {
try {
//creating server object
serversocket = new ServerSocket(6666);
socket = serversocket.accept();
//get input stream through the socket object from buffer
buffread = new BufferedReader(new InputStreamReader(socket.getInputStream()));
filename = buffread.readLine();
findFile(filename);
} catch (IOException ex) {
//System.err.println(ex);
ex.printStackTrace();
}
}
public static void main(String[] args) {
TCPServer serverObject = new TCPServer();
}
}
Client program
package tcpclient;
import java.net.*;
import java.io.*;
public class TCPClient {
BufferedReader bffread, bffinput;
String fileInput;
public TCPClient() {
try {
//Creating socket
Socket socket = new Socket("localhost", 6666);
System.out.println("Enter filename");
bffinput = new BufferedReader(new InputStreamReader(System.in));
OutputStream outputObject = socket.getOutputStream();
} catch (Exception ex) {
System.out.println("Unhandled exception caught");
}
}
public static void main(String[] args) {
TCPClient clientObject = new TCPClient();
}
}
Exception stack
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at tcpserver.TCPServer.<init>(TCPServer.java:38)
at tcpserver.TCPServer.main(TCPServer.java:47)*
Any help/suggestion is appreciated. Thanks in advance
Your server accepts the connection, but never sends anything back. The "200 OK" message gets written to stdout, not to the socket. Then the server terminates, closing the connection. At that time the client, still waiting for data, gets the exception.
I guess you want to send "200 OK" the client. So you have to pass the socket, or at least the OutputStream of the socket to findFile(), and write the response into that.
Alternatively, and a bit cleaner: return the response string from findFile(), and send it in the calling method, so findFile() doesn't even need to know about sending the response.
You should also close the socket in the block where you open it, so that data that might still be in a buffer in memory will be sent.
Client
Your client programme is not reading anything from console and sending it over to socket.
Change it to something like this..
public TCPClient() {
try {
//Creating socket
Socket socket = new Socket("localhost", 6666);
System.out.println("Enter filename");
bffinput = new BufferedReader(new InputStreamReader(System.in));
String filename = bffinput.readLine();
OutputStream outputObject = socket.getOutputStream();
// send filename over socket output stream
outputObject.write(value.getBytes());
} catch (Exception ex) {
System.out.println("Unhandled exception caught");
}
}

TCP socket connection

I'm new to the network communication and I'm trying to build client-server application.
protected void init(){
Server myServer = new Server();
Client myClient = new Client();
}
That's my Client class:
public class Client {
public Client() {
init();
}
private void init() {
Socket echoSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
DataInputStream stdIn = new DataInputStream(System.in);
try {
echoSocket = new Socket("localhost", 1234);
os = new DataOutputStream(echoSocket.getOutputStream());
is = new DataInputStream(echoSocket.getInputStream());
os.writeInt(stdIn.readInt());
echoSocket.getOutputStream().close();
echoSocket.getInputStream().close();
echoSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
And that's server:
public class Server {
public Server() {
init();
}
private void init() {
try {
boolean run = true;
ServerSocket ss = new ServerSocket(1234);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readInt());
s.getInputStream().close();
s.getOutputStream().close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
First of all:
Can I initialize client and server simply like i did? new Server() and new Client()?
Question 2:
Is it important what i initialize at first? client or server?
Question 3:
When i compile this code with client first initialized, i become Connection refused: connect. I know it means that there is no listening socket running on the port you are trying to connect to. That's why server must go first, i think. Is it so? can i fix it using setSoTimeout and how?
Question 4:
When i compile it with server and then client, output is nothing. And i think it has nothing to do with client, because if i try to print "1", for example, it doesn't work either. I think it just waits for the client and does nothing that goes after. How can i fix this? maybe setSoTimeout goes here too?
You can't have both client and server in the same thread.
As you already have observed, the server accepts the connection and tries to read something. It doesn't know that the client is running in the very same thread.
Either make a multi-threaded application, where client and server have their own thread. Or make two prgrams that run independently of each other. The latter would be also the "normal case".
Make two different projects, first run server than client.
Server will write on console "Server started" than run client it will ask your name, type your name press ok . Your name will be sent to server and server will reply saying hello to you.
Here is server code
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Server {
public static void main(String[] args) {
try{
ServerSocket ss= new ServerSocket(2224);
System.out.println("Serever started");
while(true)
{
Socket s=ss.accept();
InputStream is=s.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
OutputStream os=s.getOutputStream();
PrintWriter pw=new PrintWriter(os);
String name=br.readLine();
String message="Hello "+name+"from server";
pw.println(message);
pw.flush();
}
}
catch(Exception exp)
{
System.out.println("Excepttion occured");
}
}
}
Here is client code
import java.net.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket s=new Socket("localhost",2224);
InputStream is=s.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
OutputStream os=s.getOutputStream();
PrintWriter pw=new PrintWriter(os,true);
String message = JOptionPane.showInputDialog("Give your name");
pw.println(message);
pw.flush();
String servermessage = br.readLine();
JOptionPane.showMessageDialog(null, servermessage);
s.close();
}
}

How create servers and clients with socket?

I need to build an app where I have four servers running and one client send some packages to these servers, but these servers have to keep running all the time receiving something from the client(sender).
So I create the both classes, client and server:
public class Server {
public Event receive(int port) {
Evento event = null;
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
evento = (Evento) ois.readObject();
is.close();
s.close();
ss.close();
}catch(Exception e){
System.out.println(e);
}
return event;
}
}
public class Client {
public void send(Event event, int port) {
try {
Socket s = new Socket("localhost", 2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(event);
oos.close();
os.close();
s.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
But as I said, I need these servers keep running all the time, if I test once, that's ok, but twice or more, don't.
How could I do that ?
The standard pattern is to create one thread for each connection. If you use one thread you can only read from one blocking connection.
just add a do while block around the readObject.
As a break condition you can check whether the message is something like "exit"..
cheers

Arraylist through tcp in java?

How can I send an arraylist through tcp in Java?
I need to send an arraylist of integers, from client to server and vice verse.
Thanxx
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
public class SerializeOverSocket {
private static ExecutorService executorService =
Executors.newSingleThreadExecutor();
public static void main(String[] args) throws Exception {
// Start a server to listen for a client
executorService.submit(new Server());
Thread.sleep(100);
// Send an ArrayList from a client
ArrayList<Integer> integers =
new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
Socket s = new Socket();
s.connect(new InetSocketAddress("localhost", 1234));
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(integers);
s.close();
}
static class Server implements Runnable {
public void run() {
try {
ServerSocket server = new ServerSocket(1234);
Socket clientSocket = server.accept();
ObjectInputStream in =
new ObjectInputStream(clientSocket.getInputStream());
Object o = in.readObject();
System.out.println("Received this object on the server: " + o);
clientSocket.close();
server.close();
executorService.shutdown();
} catch (IOException e) {
// TODO: Write me
throw new UnsupportedOperationException("Not written");
} catch (ClassNotFoundException e) {
// TODO: Write me
throw new UnsupportedOperationException("Not written");
}
}
}
}
The simplest way would be:
serialize to byte[] (or directly write to the output stream as shown by Ryan)
open a socket
write the bytes in the client
receive the bytes on the server
deserialize the byte[] (or read from the stream as shown by Ryan)
To handle serialization use ObjectOutputStream and ObjectInputStream. You can also use commons-lang SerializationUtils which give you one-liners to serialize and deserialize.
Look into serialization. Java's ArrayList object already implements the serializable interface, so you just have to learn how to use it.
http://www.java2s.com/Tutorial/Java/0140__Collections/ArrayListimplementstheemptySerializableinterface.htm
That example shows how to write an ArrayList to a file, but the same concept applys to sending it over sockets.
I know this is a very old question, but perhaps newer versions of the Java lang have make sending objects through TCP easier than in the past. And this is simpler than it seems. Checkout the following example:
Client side of your TCP connection:
//All the imports here
//...
public class Client {
private ObjectInputStream in; //The input stream
private ObjectOutputStream out; //The output stream
private Socket socket; //The socket
public Client(){
//Initialize all the members for your
//Client class here
}
//Using your sendData method create
//an array and send it through your
//output stream object
public void sendData(int[] myIntegerArray) {
try {
//Note the out.writeObject
//this means you'll be passing an object
//to your server
out.writeObject(myIntegerArray);
out.flush();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Server side of your TCP connection:
//All the imports here
//...
public class Server {
private ObjectInputStream in; //The input stream
private ObjectOutputStream out; //The output stream
private ServerSocket serverSocket; //The serverSocket
public Server(){
//Initialize all the members for your
//Server class here
}
//Using your getData method create
//the array from the serialized string
//sent by the client
public void getData() {
try {
//Do a cast to transform the object
//into the actual object you need
//which is an Integer array
int[] data = (int[]) in.readObject();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

Categories

Resources