package socketserver;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Nurhak
*/
public class SocketServer extends Thread {
public static final int PORT = 8080;
public Socket socket = null;
public SocketServer(Socket socket) {
this.socket = socket;
}
public static String findValue(int param) {
String text = null;
switch (param) {
case 0:
text = "Zero";
break;
case 1:
text = "One";
break;
case 2:
text = "Two";
break;
case 3:
text = "Three";
break;
case 4:
text = "Four";
break;
case 5:
text = "Five";
break;
case 6:
text = "Six";
break;
case 7:
text = "Seven";
break;
case 8:
text = "Eight";
break;
case 9:
text = "Nine";
break;
default:
text = "Unexpected value";
break;
}
return text;
}
@Override
public void run() {
BufferedReader br = null;
PrintWriter pr = null;
try {
System.out.println("Thread name: " + this.getName()
+ " Port Nr: " + socket);
br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
pr = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
String data = br.readLine();
while (data != null) {
System.out.println(data);
Integer i = new Integer(data);
int param = i.intValue();
String text = findValue(param);
System.out.println(text);
data = br.readLine();
}
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server is started!" + serverSocket);
for (;;) {
Socket clientSocket = serverSocket.accept();
System.out.println("Connection accepted!" + clientSocket);
clientSocket.setSoTimeout(1000);
new SocketServer(clientSocket).start();
}
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Nurhak
*/
public class SocketServer extends Thread {
public static final int PORT = 8080;
public Socket socket = null;
public SocketServer(Socket socket) {
this.socket = socket;
}
public static String findValue(int param) {
String text = null;
switch (param) {
case 0:
text = "Zero";
break;
case 1:
text = "One";
break;
case 2:
text = "Two";
break;
case 3:
text = "Three";
break;
case 4:
text = "Four";
break;
case 5:
text = "Five";
break;
case 6:
text = "Six";
break;
case 7:
text = "Seven";
break;
case 8:
text = "Eight";
break;
case 9:
text = "Nine";
break;
default:
text = "Unexpected value";
break;
}
return text;
}
@Override
public void run() {
BufferedReader br = null;
PrintWriter pr = null;
try {
System.out.println("Thread name: " + this.getName()
+ " Port Nr: " + socket);
br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
pr = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
String data = br.readLine();
while (data != null) {
System.out.println(data);
Integer i = new Integer(data);
int param = i.intValue();
String text = findValue(param);
System.out.println(text);
data = br.readLine();
}
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server is started!" + serverSocket);
for (;;) {
Socket clientSocket = serverSocket.accept();
System.out.println("Connection accepted!" + clientSocket);
clientSocket.setSoTimeout(1000);
new SocketServer(clientSocket).start();
}
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Comments
Post a Comment