// Server.java
package com.objectmentor.clientserver.nonthreaded;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import common.MessageUtils;
public class Server implements Runnable
{
ServerSocket serverSocket;
volatile boolean keepProcessing = true;
public Server(int port, int millisecondsTimeout) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(millisecondsTimeout);
}
public void run()
{
System.out.printf("Server Starting\n");
while (keepProcessing)
{
try
{
System.out.printf("accepting client\n");
Socket socket = serverSocket.accept();
System.out.printf("got client\n");
process(socket);
}
catch (Exception e)
{
handle(e);
}
}
}
private void handle(Exception e)
{
if (!(e instanceof SocketException)) {
e.printStackTrace();
}
}
public void stopProcessing()
{
keepProcessing = false;
closeIgnoringException(serverSocket);
}
void process(Socket socket)
{
if (socket == null)
return;
try
{
System.out.printf("Server : getting message\n");
String message = MessageUtils.getMessage(socket);
System.out.printf("Server : got message : %s\n", message);
Thread.sleep(1000);
System.out.printf("Server : sending reply : %s \n", message);
MessageUtils.sendMessage(socket, "Processed: " + message);
System.out.printf("Server : sent\n");
closeIgnoringException(socket);
}
catch (Exception e)
{
e.printStackTrace();
}
}
// 다중스레드 버전
/*
void process(Socket socket)
{
if (socket == null)
return;
Runnable clientHandler = new Runnable() {
public void run()
{
try
{
System.out.printf("Server : getting message\n");
String message = MessageUtils.getMessage(socket);
System.out.printf("Server : got message : %s\n", message);
Thread.sleep(1000);
System.out.printf("Server : sending reply : %s \n", message);
MessageUtils.sendMessage(socket, "Processed: " + message);
System.out.printf("Server : sent\n");
closeIgnoringException(socket);
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
Thread clientConnection = new Thread(clientHandler);
clientConnection.start();
}
*/
private void closeIgnoringException(Socket socket)
{
if (socket != null)
try
{
socket.close();
}
catch (IOException ignore)
{
}
}
private void ccloseIgnoringException(ServerSocket serverSocket)
{
if (serverSocket != null)
try
{
serverSocket.close();
}
catch (IOException ignore)
{
}
}
}
// ClientTest.java
유실됨..
// MessageUtils.java
package common;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MessageUtils
{
public static void sendMessage(Socket socket, String message) throws IOException
{
OutputStream stream = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(stream);
oos.writeUTF(message);
oos.flush();
}
public static String getMessage(Socket socket) throws IOException
{
InputStream stream = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(stream);
return ois.readUTF();
}
}
'Java' 카테고리의 다른 글
OpenJDK 설치 (0) | 2019.08.08 |
---|---|
CentOS 자바 설치 (0) | 2019.01.04 |
Java System.loadLibrary 로 native module 로드시 에러 (0) | 2017.11.17 |
[spring boot] websocket 예제 중 html 부분 (0) | 2017.10.18 |
Thread 기본, IO, NIO 기본 내용들 (0) | 2011.01.03 |
Java에서 XML 다루기 DOM, SAX, JDOM, StAX (3) | 2010.02.06 |
간단하게 gzip 압축 및 해제 해보기 (0) | 2010.02.03 |
Java Thread (0) | 2010.01.24 |