Java Socket Example

// SS.java
//
 
import java.net.*;
import java.io.*;
 
public class SS
{
	static final int PORT = 5000;
 
	public static void main(String argv[])
	{
		System.out.println("SS : Port : " + PORT);
		ServerSocket sSock;
		while (true)
		{
		try 
		{
			sSock = new ServerSocket(PORT);
			Socket sSockConnected = sSock.accept();
			InetAddress ip = sSockConnected.getInetAddress();
			System.out.println("Connected to "+ip.toString());
			OutputStream os = sSockConnected.getOutputStream();
			byte buf[] = new byte[10];
			buf[0] = (byte) 0x61;
			buf[1] = (byte) 0x62;
			buf[2] = (byte) 0x63;
			buf[3] = (byte) 0x64;
			buf[4] = (byte) 0x65;
			buf[5] = (byte) 0x66;
			buf[6] = (byte) 0x67;
			buf[7] = (byte) 0x68;
			buf[8] = (byte) 0;
			while (true)
			{
				os.write(buf);
				try
				{
					Thread.sleep(1000);
				}
				catch (InterruptedException ie)
				{
					System.err.println("*Interrupt*");
					System.exit(1);
				}
			}
		}
		catch (java.io.IOException ioEx)
		{
			System.err.println("IO Error in new ServerSocket");
			System.err.println(ioEx.toString());
		}
		}
	}
}

javasocketexample.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0