GGYU

object 전송 본문

프로그래밍/JAVA

object 전송

GANADARA 2018. 7. 19. 22:26

Student.java

import java.io.Serializable;

public class Student implements Serializable {


private static final long serialVersionUID = 5950169519310163575L;

private int id;

private String name;


public Student(int id, String name) {

this.id = id;

this.name = name;

}


public int getId() {

return id;

}


public void setId(int id) {

this.id = id;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}


public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;


Student student = (Student) o;


if (id != student.id) return false;

if (name != null ? !name.equals(student.name) : student.name != null) return false;


return true;

}


public int hashCode() {

return id;

}


public String toString() {

return "Id = " + getId() + " ; Name = " + getName();

}





Client.java

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.Socket;

import java.net.SocketException;



public class Client {

private Socket socket = null;

private ObjectInputStream inputStream = null;

private ObjectOutputStream outputStream = null;

private boolean isConnected = false;


public Client() {


}


public void communicate() {


while (!isConnected) {

try {

socket = new Socket("localHost", 4445);

System.out.println("Connected");

isConnected = true;

outputStream = new ObjectOutputStream(socket.getOutputStream());

Student student = new Student(1, "Bijoy");

System.out.println("Object to be written = " + student);

outputStream.writeObject(student);


} catch (SocketException se) {

se.printStackTrace();

// System.exit(0);

} catch (IOException e) {

e.printStackTrace();

}

}

}


public static void main(String[] args) {

Client client = new Client();

client.communicate();

}

}




Server.java

import java.io.*;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.SocketException;


public class Server {

private ServerSocket serverSocket = null;

private Socket socket = null;

private ObjectInputStream inStream = null;


public Server() {


}


public void communicate() {

try {

serverSocket = new ServerSocket(4445);

socket = serverSocket.accept();

System.out.println("Connected");

inStream = new ObjectInputStream(socket.getInputStream());


Student student = (Student) inStream.readObject();

System.out.println("Object received = " + student);

socket.close();


} catch (SocketException se) {

System.exit(0);

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException cn) {

cn.printStackTrace();

}

}


public static void main(String[] args) {

Server server = new Server();

server.communicate();

}

}


'프로그래밍 > JAVA' 카테고리의 다른 글

console 입력  (0) 2019.04.21
reflection  (0) 2018.07.19
collection sort  (0) 2018.07.19
체인코드  (0) 2018.07.19
sha512 예제  (0) 2018.07.19
Comments