Java--Socket 통신(클라이언트 서버 양방향)
두 공정의 읽기와 쓰기 조작 스레드 종류는 기본적으로 완전히 같다
서버:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static final int PORT = 8000;//
public static void main(String[] args) {
Server server = new Server();
server.init();
}
public void init() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(PORT);
while (true) {
Socket client = serverSocket.accept();
//
new Thread(new ReadHandlerThread(client)).start();
new Thread(new WriteHandlerThread(client)).start();
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(serverSocket != null){
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
*
*/
class ReadHandlerThread implements Runnable{
private Socket client;
public ReadHandlerThread(Socket client) {
this.client = client;
}
@Override
public void run() {
DataInputStream dis = null;
try{
while(true){
//
dis = new DataInputStream(client.getInputStream());
String reciver = dis.readUTF();
System.out.println(" :" + reciver);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(dis != null){
dis.close();
}
if(client != null){
client = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
*
*/
class WriteHandlerThread implements Runnable{
private Socket client;
public WriteHandlerThread(Socket client) {
this.client = client;
}
@Override
public void run() {
DataOutputStream dos = null;
BufferedReader br = null;
try{
while(true){
//
dos = new DataOutputStream(client.getOutputStream());
System.out.print(" :\t");
//
br = new BufferedReader(new InputStreamReader(System.in));
String send = br.readLine();
//
dos.writeUTF(send);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(dos != null){
dos.close();
}
if(br != null){
br.close();
}
if(client != null){
client = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
클라이언트:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
public static final String IP = "localhost";//
public static final int PORT = 8000;//
public static void main(String[] args) {
handler();
}
private static void handler(){
try {
// Socket,
Socket client = new Socket(IP, PORT);
// , ,
new Thread(new ReadHandlerThread(client)).start();
new Thread(new WriteHandlerThread(client)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
*
*/
class ReadHandlerThread implements Runnable{
private Socket client;
public ReadHandlerThread(Socket client) {
this.client = client;
}
@Override
public void run() {
DataInputStream dis = null;
try {
while(true){
//
dis = new DataInputStream(client.getInputStream());
String receive = dis.readUTF();
System.out.println(" : " + receive);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(dis != null){
dis.close();
}
if(client != null){
client = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
*
*/
class WriteHandlerThread implements Runnable{
private Socket client;
public WriteHandlerThread(Socket client) {
this.client = client;
}
@Override
public void run() {
DataOutputStream dos = null;
BufferedReader br = null;
try {
while(true){
//
dos = new DataOutputStream(client.getOutputStream());
System.out.print(" : \t");
//
br = new BufferedReader(new InputStreamReader(System.in));
String send = br.readLine();
//
dos.writeUTF(send);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(dos != null){
dos.close();
}
if(br != null){
br.close();
}
if(client != null){
client = null;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.