my code stock.com

-

Snippet options

Download: Download snippet as simpletextserver.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;



public class SimpleTextServer {

	
	public static void main(String[] args) throws Exception {
	
	int port = 6789;
	InetAddress ip;
	SocketConn ourConnection;
	ourConnection = new SocketConn(port);
	try{
		ip= InetAddress.getLocalHost();
	System.out.println("Listening on: "+ port + ip.getHostAddress());
	}catch(UnknownHostException e)
	{
		e.printStackTrace();
	}
	ourConnection.run();
	


	}




}


class SocketConn {

	 static String fromClient;         
	 static String toClient;  
	static  ServerSocket welcomeSocket;
	static DataOutputStream outToClient;
	 static BufferedReader inFromClient;
	static Socket connectionSocket;
	static int port;
	static int sensorValue;
	static String adm_status;
	public SocketConn(int portval)
	{  port = portval;}
	
	 public void run(){
		try {
			welcomeSocket = new ServerSocket(port);
			System.out.println("IP:"+ welcomeSocket.getLocalSocketAddress().toString());
		} catch (IOException e) {
			e.printStackTrace();}  
	 while(true)         
	 {         
			try {
				connectionSocket = welcomeSocket.accept();
				inFromClient =          
						 new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
				 
				  outToClient = new DataOutputStream(connectionSocket.getOutputStream());

				 fromClient = inFromClient.readLine();
				 if(fromClient.equals("1")||fromClient.equals("0"))
				 {
					 adm_status = fromClient.toString();
					 outToClient.writeBytes(adm_status+'\n');
				 }
				 else
				 {
					 outToClient.writeBytes(adm_status+'\n');
				 }
				 
				 System.out.println("R:[ "+fromClient+"]  SENT["+adm_status+"]");

				 
				 
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			  
			 
	 }
			 
	 

}
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.