Created
April 6, 2016 11:38
-
-
Save sakthiinfotec/a839e83eb4331cad72456b7207a0c742 to your computer and use it in GitHub Desktop.
A Simple TCP Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.sakthiinfotec.server; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| import java.net.ServerSocket; | |
| import java.net.Socket; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| public class TCPServer { | |
| private static final Logger LOGGER = LoggerFactory.getLogger(TCPServer.class); | |
| public static void main(String[] args) { | |
| String clientMessage = null; | |
| ServerSocket serverSocket = null; | |
| LOGGER.info("Starting TCP Server ..."); | |
| try { | |
| serverSocket = new ServerSocket(9090); | |
| while (true) { | |
| Socket clientSocket = serverSocket.accept(); | |
| BufferedReader inFromClient = new BufferedReader( | |
| new InputStreamReader(clientSocket.getInputStream())); | |
| clientMessage = inFromClient.readLine(); | |
| LOGGER.info("Client msg:" + clientMessage); | |
| } | |
| } catch (IOException ioe) { | |
| LOGGER.error("Error in communication with client", ioe); | |
| } finally { | |
| Utils.closeSocket(serverSocket); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment