336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
Server
package lg.tmte.network;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class RunManager {
public static void main(String[] args) {
ServerSocket serverSocket;
DataInputStream dis;
DataOutputStream dos;
InputStream is;
OutputStream os;
try {
serverSocket = new ServerSocket(8080);
while(true) {
Socket socket = serverSocket.accept();
System.out.println("Connected");
is = socket.getInputStream();
dis = new DataInputStream(is);
Thread thread = new Thread(new MultiServer(socket,dis.readUTF()));
thread.start();
thread.join();
os = socket.getOutputStream();
dos = new DataOutputStream(os);
dos.writeUTF("done");
socket.close();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MultiServer implements Runnable{
Socket socket;
String id;
public MultiServer(Socket socket, String id) {
this.socket = socket;
this.id = id;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("I'm "+id);
}
}
Client
package lg.tmte.network;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientThread {
// Runnable r1 = new RunnableThread("Thread1");
// Runnable r2 = new RunnableThread("Thread2");
//
// Thread t1 = new Thread(r1);
// Thread t2 = new Thread(r2);
/*
* Bus Info Arr
* */
public void start() {
String[] bus01Arr = {"40","50","88","98","145"};
String[] bus02Arr = {"30","54","78","128","133"};
String[] bus03Arr = {"20","22","45","87","100"};
Runnable r1 = new ClientServer("127.0.0.1",8089,"BUS01",bus01Arr);
Runnable r2 = new ClientServer("127.0.0.1",8089,"BUS02",bus02Arr);
Runnable r3 = new ClientServer("127.0.0.1",8089,"BUS03",bus03Arr);
}
public static void main(String[] args) {
new ClientThread().start();
}
}
class ClientServer implements Runnable{
String SERVERIP = "";
int PORT;
String busName = "";
Socket socket = null;
String[] arr = null;
DataOutputStream dos = null;
OutputStream os = null;
public ClientServer(String SERVERIP, int PORT, String busName,String[] arr) {
this.SERVERIP = SERVERIP;
this.PORT = PORT;
this.busName = busName;
this.arr = arr;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
while(true) {
socket = new Socket(SERVERIP,PORT);
os = socket.getOutputStream();
dos = new DataOutputStream(os);
/* 파일을 읽어서 버스번호가 맞는 것만 계속 보낸다. */
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Bus implements Comparable<Bus>{
String name;
String location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Bus(String name, String location) {
super();
this.name = name;
this.location = location;
}
@Override
public String toString() {
return "Bus [name=" + name + ", location=" + location + "]";
}
@Override
public int compareTo(Bus o) {
// TODO Auto-generated method stub
return o.getLocation().compareTo(this.getLocation()) >= 0 ? -1 : 1;
}
}
Thread에 값을 넘기고 싶을때는 생성자를 통해 넘기고,
ArrayList와 같은 객체를 넘겨주면 Thread 끼리 공유를 한다.
'IT > Java' 카테고리의 다른 글
프로그래머스 DFS 네트워크 (0) | 2020.08.10 |
---|---|
자바 EXE 파일 실행 및 데이터 전달 (0) | 2020.07.14 |
Java HashMap Controller (0) | 2020.06.22 |
Java ArrayController (0) | 2020.06.22 |
Java ArrayList Controller (0) | 2020.06.22 |