💡 채팅 프로그램
✔️ 결과 화면
✔️ 서버 소켓(Server Socket) 소스코드
[Server]
package com.kh.day17.network.socket.chat;
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;
import java.util.Scanner;
public class ChattingServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
int port = 8888;
InputStream is = null;
OutputStream os = null;
DataInputStream dis = null;
DataOutputStream dos = null;
Scanner sc = new Scanner(System.in);
try {
System.out.println("채팅 서버를 구동중입니다..");
serverSocket = new ServerSocket(port);
System.out.println("채팅 서버를 구동하였습니다.");
System.out.println("클라이언트의 접속을 기다리고 있습니다.");
Socket socket = serverSocket.accept();
System.out.println("클라이언트가 접속하였습니다.");
is = socket.getInputStream();
os = socket.getOutputStream();
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
System.out.println("채팅이 시작되었습니다.");
while(true) {
System.out.print("서버(나) : ");
String sendMsg = sc.nextLine();
dos.writeUTF(sendMsg);
dos.flush();
String recMsg = dis.readUTF();
System.out.printf("클라이언트(상대) : %s\n", recMsg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
✔️ 클라이언트 소켓(Client Socket) 소스코드
[Client]
package com.kh.day17.network.socket.chat;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class ChattingClient {
public static void main(String[] args) {
String address = "127.0.0.1";
int port = 8888;
InputStream is = null;
OutputStream os = null;
DataInputStream dis = null;
DataOutputStream dos = null;
Scanner sc = new Scanner(System.in);
try {
System.out.println("서버에 연결중입니다.");
Socket socket = new Socket(address, port);
System.out.println("채팅서버에 접속하였습니다.");
is = socket.getInputStream();
os = socket.getOutputStream();
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
System.out.println("서버와의 채팅을 시작합니다.");
while(true) {
String recMsg = dis.readUTF();
System.out.printf("서버(상대) : %s\n", recMsg);
System.out.print("클라이언트(나) : ");
String sendMsg = sc.nextLine();
dos.writeUTF(sendMsg);
dos.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
💡 계산기 프로그램
✔️ 결과 화면
✔️ 서버 소켓(Server Socket) 소스코드
package com.kh.day17.network.socket.calculator;
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;
import java.util.Scanner;
public class CalCulatorServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
int port = 8888;
InputStream is = null;
OutputStream os = null;
DataInputStream dis = null;
DataOutputStream dos = null;
Scanner sc = new Scanner(System.in);
try {
System.out.println("서버 구동중입니다.");
for(int i = 1; i <= 50; i++) {
System.out.print("=");
Thread.sleep(10);
}
System.out.print("100%");
System.err.println();
serverSocket = new ServerSocket(port);
System.out.println("클라이언트 연결을 기다리고 있습니다...");
Socket socket = serverSocket.accept();
System.out.println("클라이언트와 연결되었습니다.");
is = socket.getInputStream();
os = socket.getOutputStream();
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
while(true) {
String recvMsg = dis.readUTF();
if(recvMsg.equalsIgnoreCase("exit")) {
System.out.println("클라이언트가 종료하였습니다.");
break;
}
System.out.printf("받은 메세지 : %s\n", recvMsg);
String [] msgArrs = recvMsg.split(" ");
if (msgArrs.length != 3) {
String errMsg = "end";
dos.writeUTF(errMsg);
continue;
}
int num1 = Integer.parseInt(msgArrs[0]);
int num2 = Integer.parseInt(msgArrs[2]);
String operator = msgArrs[1];
String result = "";
switch(operator) {
case "+" :
result = String.valueOf(num1 + num2);
break;
case "-" :
result = String.valueOf(num1 - num2);
break;
case "*" :
result = num1 * num2 + "";
break;
case "/" :
result = num1 / num2 + "";
break;
case "%" :
result = num1 % num2 + "";
break;
}
dos.writeUTF(result);
dos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
✔️ 클라이언트 소켓(Client Socket) 소스코드
package com.kh.day17.network.socket.calculator;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class CalCulatorClient {
public static void main(String[] args) {
String address = "127.0.0.1";
int port = 8888;
InputStream is = null;
OutputStream os = null;
DataInputStream dis = null;
DataOutputStream dos = null;
Scanner sc = new Scanner(System.in);
try {
Socket socket = new Socket(address, port);
System.out.println("서버와 연결되었습니다.");
is = socket.getInputStream();
os = socket.getOutputStream();
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
for(;;) {
System.out.print("계산식(빈칸으로 띄어 입력, 예 24 + 42) >> ");
String sendMsg = sc.nextLine();
dos.writeUTF(sendMsg);
if(sendMsg.equalsIgnoreCase("exit")) {
System.out.println("종료하였습니다.");
break;
}
dos.flush();
String recvMsg = dis.readUTF();
if(recvMsg.equalsIgnoreCase("end")) {
System.out.println("잘못 입력하셨습니다. 형식에 맞게 입력해주세요.");
continue;
}
System.out.printf("계산 결과 : %s\n", recvMsg);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
💡 야구 게임 프로그램
✔️ 결과 화면
✔️ 서버 소켓(Server Socket) 소스코드
[Server]
package com.kh.day17.network.socket.baseball;
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;
import java.util.Date;
import java.util.Scanner;
public class BaseballServer {
public static void main(String[] args) {
int port = 8888;
ServerSocket serverSocket = null;
Date date = new Date();
Scanner sc = new Scanner(System.in);
InputStream is = null;
OutputStream os = null;
DataInputStream dis = null;
DataOutputStream dos = null;
int [] numbers = new int[3];
try {
serverSocket = new ServerSocket(port);
System.out.println("서버소켓을 생성하였습니다.");
Thread.sleep(1500);
System.out.println("클라이언트의 접속을 기다립니다.");
Socket socket = serverSocket.accept();
System.out.println("클라이언트가 접속했습니다.");
System.out.print("서버 숫자 -> ");
for(int i = 0; i < 3; i++) {
numbers[i] = (int)(Math.random()*9+1);
for(int j = 0; j < i; j++) {
if(numbers[j] == numbers[i]) {
i--;
break;
}
}
}
System.out.printf("%d %d %d\n", numbers[0], numbers[1], numbers[2]);
System.out.println("서버 준비 완료");
is = socket.getInputStream();
os = socket.getOutputStream();
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
while(true) {
String recvMsg = dis.readUTF();
System.out.printf("받기 : %s\n", recvMsg);
String [] inputNums = recvMsg.split(" ");
int strike = 0;
int ball = 0;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(numbers[i] == Integer.parseInt(inputNums[j])) {
if(i == j) {
strike++;
} else {
ball++;
}
}
}
}
String result = strike + "스트라이크" + ball + "볼";
dos.writeUTF(result);
dos.flush();
if(strike == 3) {
System.out.println("아웃! 게임 종료");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
✔️ 클라이언트 소켓(Client Socket) 소스코드
[Client]
package com.kh.day17.network.socket.baseball;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class BaseballClient {
public static void main(String[] args) {
String address = "127.0.0.1";
int port = 8888;
Scanner sc = new Scanner(System.in);
InputStream is = null;
OutputStream os = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try {
Socket socket = new Socket(address, port);
System.out.println("소켓을 생성하였습니다.");
System.out.println("게임 준비 완료");
is = socket.getInputStream();
os = socket.getOutputStream();
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
while(true) {
System.out.print("--> ");
String input = sc.nextLine();
dos.writeUTF(input);
os.flush();
String recvMsg = dis.readUTF();
System.out.println(recvMsg);
if(recvMsg.startsWith("3")) {
System.out.println("축하합니다. 맞췄습니다.");
break;
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
글 잘 봤습니다, 감사합니다.