본문 바로가기

Java/java 기초

쓰레드를 활용한 오브젝트 전송하기

서버 부분 

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();
		}

		
	}
}

 

 

 

결과 창 - 클라이언트 부분 

 

결과창 - 서버 부분