I am new to Socket programming.I want to send an Object of class Person to the server using Sockets. There is an exception of Server Socket.
Class person is as follows:
import java.awt.Image;
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private String id;
public Person() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Person(String name, String id) {
super();
this.name = name;
this.id = id;
}
}
I first run my server . The server code is as follows:
import java.io.*;
import java.net.*;
public class Server extends Thread {
ServerSocket serverSocket;
Socket socket;
ObjectInputStream inStream=null;
ObjectOutputStream outStream=null;
public void run(){
try{
serverSocket=new ServerSocket(2004);
socket=serverSocket.accept();
this.outStream=new ObjectOutputStream(socket.getOutputStream());
outStream.flush();
this.inStream=new ObjectInputStream(socket.getInputStream());
while(true){
System.out.println("recieving data");
Person person=(Person)this.inStream.readObject();
//queue.put(person);
System.out.println(person.getName()+" "+person.getId());
}
}
catch(EOFException e){
}
catch(IOException e){
e.printStackTrace();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public static void main(String args[]){
new Server().start();
}
}
Then I run my client. Its code is as follows.
import java.io.*;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.tree.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.LinkedList;
public class Client {
Socket socket;
ObjectInputStream inStream=null;
ObjectOutputStream outStream=null;
String ip="localhost";
boolean connected=false;
public Client(String ip){
this.ip=ip;
}
public Client(){
}
public static void main(String args[]){
new Client().connect();
}
public boolean connect(){
try {
socket = new Socket(ip, 2004);
System.out.println("Client starting");
this.outStream=new ObjectOutputStream(socket.getOutputStream());
outStream.flush();
this.inStream=new ObjectInputStream(socket.getInputStream());
String name;
System.out.println("sending data");
outStream.writeObject(new Person("faisal ","232323"));
}catch(EOFException e){
JOptionPane.showMessageDialog(null,"EOFException");
System.out.println("I am inside Connector EOFException");
connected=false;
//System.out.println("PoolRunner Client side Disconnected");
}
catch (IOException e) {
JOptionPane.showMessageDialog(null, "IOException");
System.out.println("I am inside Connector IOException");
System.out.println(e);
connected=false;
}
finally{
return(connected);
}
}
public ObjectInputStream getObjectInputStream(){
return this.inStream;
}
public ObjectOutputStream getObjectOutputStream(){
return this.outStream;
}
public void disconnect(){
try{
this.inStream.close();
this.outStream.close();
this.socket.close();
}catch(IOException e){
JOptionPane.showMessageDialog(null,"Problem in closing streams or socket");
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Problem in closing streams or socket");
e.printStackTrace();
}
}
}
Following Exception is thrown at the server side.
java.net.SocketException: Connection reset