Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SingleSocketServer: A Server which SERVES One Socket at a Time
- // Runs on local IP address 10.64.18.11
- import java.net.*;
- import java.io.*;
- import java.util.*;
- public class TCP_Echo_Server {
- static ServerSocket servSock;
- protected final static int port = 1024 + 7;
- static Socket clientSock;
- //static boolean first;
- //static StringBuffer process;
- public static void main(String[] args) {
- try{
- servSock = new ServerSocket(port);
- System.out.println("Single SocketServer Initialized");
- int character;
- while (true) {
- clientSock = servSock.accept();
- BufferedInputStream is = new BufferedInputStream(clientSock.getInputStream());
- InputStreamReader isr = new InputStreamReader(is);
- String process = "";
- while((character = isr.read()) != 13) {
- process = process + (char)character;
- }
- System.out.println("Received from Client: " + process);
- String returnCode = "Echo server repsonded: " + process +(char) 13;
- BufferedOutputStream os = new BufferedOutputStream(clientSock.getOutputStream());
- OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
- osw.write(returnCode);
- osw.flush();
- }
- }
- catch (IOException e) { e.printStackTrace();}
- try {
- clientSock.close();
- }
- catch (IOException e) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement