서버 부분
package main;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class ObjectTread extends Thread {
Socket socket;
int memberNum;
public ObjectTread(Socket socket, int memberNum) {
this.socket = socket;
this.memberNum = memberNum;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
while (true) {
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
MemberDto dto = (MemberDto)ois.readObject();
System.out.println("client로 부터 Obj: " + dto.toString());
dto.setNumber(memberNum);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(dto);
oos.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DTO 부분 - 서버와 클라이언트의 DTO가 동일해야하며 패키지명도 같아야 한다.
package main;
import java.io.Serializable;
// 직렬화: Network 전송시
public class MemberDto implements Serializable{
private int number;
private String name;
public MemberDto() {
}
public MemberDto(int number, String name) {
super();
this.number = number;
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MemberDto [number=" + number + ", name=" + name + "]";
}
}
메인 클래스
package main;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class mainClass {
public static void main(String[] args) throws IOException {
int MemberNum = 0;
ServerSocket serSocket = new ServerSocket(9000);
while (true) {
System.out.println("대기중...");
Socket socket = serSocket.accept();
MemberNum++;
new ObjectTread(socket, MemberNum).start();
}
}
}
클라이언트 부분
DTO 부분
package main;
import java.io.Serializable;
// 직렬화: Network 전송시
public class MemberDto implements Serializable{
private int number;
private String name;
public MemberDto() {
}
public MemberDto(int number, String name) {
super();
this.number = number;
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MemberDto [number=" + number + ", name=" + name + "]";
}
}
메인 부분
package main;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;
//전송될 DTO와 패키지명이 같아야한다.
public class mainClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
InetSocketAddress sockAddr = new InetSocketAddress("192.168.2.5", 9000);
Socket socket = new Socket();
try {
socket.connect(sockAddr);
InetAddress inetAddr;
if((inetAddr = socket.getInetAddress()) != null) {
System.out.println("연결: " + inetAddr);
}else {
System.out.println("연결되지 않았습니다.");
}
while(true) {
System.out.println("이름: ");
String name = scanner.next();
MemberDto dto = new MemberDto(0, name);
// send
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(dto);
oos.flush();
// recv
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
MemberDto _dto = (MemberDto)ois.readObject();
System.out.println("Server로 부터 전송된 Object: " + _dto.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
결과 창 - 클라이언트 부분
결과창 - 서버 부분
'Java > java 기초' 카테고리의 다른 글
servlet a 태그 받아오기 (0) | 2020.01.17 |
---|---|
자바 JFRAME + JDBC + OracleDB를 활용한 카페 주문 시스템 구현하기 (3) | 2019.12.31 |
쓰레드 공부하기 (0) | 2019.12.10 |
awt 로 채팅 프로그램 구현하기 (0) | 2019.12.10 |
네트워크 공부 (0) | 2019.12.09 |