Created
April 19, 2016 06:22
-
-
Save anonymous/bf40864d6b4b5585ff95a2ecc309507f to your computer and use it in GitHub Desktop.
Socket sample Java
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
Compile client and server and then start server as follows: | |
$ java GreetingServer 6066 | |
Waiting for client on port 6066... | |
Check client program as follows: | |
Start client : | |
$ java GreetingClient localhost 6066 | |
Connecting to localhost on port 6066 | |
Just connected to localhost/127.0.0.1:6066 | |
Server says Thank you for connecting to /127.0.0.1:6066 | |
Goodbye! |
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
// File Name GreetingClient.java | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.Socket; | |
import java.util.Scanner; | |
public class GreetingClient { | |
public static void main(String[] args) { | |
String serverName = args[0]; | |
int port = Integer.parseInt(args[1]); | |
try { | |
System.out.println("CLIENT: Connecting to " + serverName + " on port " + port); | |
Socket client = new Socket(serverName, port); | |
System.out.println("CLIENT: Just connected to " + client.getRemoteSocketAddress()); | |
OutputStream outToServer = client.getOutputStream(); | |
final DataOutputStream out = new DataOutputStream(outToServer); | |
out.writeUTF("Hello from client " + client.getLocalSocketAddress()); | |
InputStream inFromServer = client.getInputStream(); | |
DataInputStream in = new DataInputStream(inFromServer); | |
String msg = in.readUTF(); | |
System.out.println("SERVER: " + msg); | |
final Scanner scanIn = new Scanner(System.in); | |
new Thread() { | |
public void run() { | |
while (true) { | |
System.out.println("Enter message : "); | |
String sWhatever = scanIn.nextLine(); | |
try { | |
out.writeUTF("\"" + sWhatever + "\""); | |
} catch (IOException e) { | |
System.out.println("SERVER: left\nGAME OVER "); | |
} | |
} | |
}; | |
}.start(); | |
while (!msg.equalsIgnoreCase("q")) { | |
msg = in.readUTF(); | |
System.out.println("SERVER: " + msg); | |
if (!msg.startsWith(" I received ")) { | |
out.writeUTF(" I received \"" + msg.replace("\"", "") + "\""); | |
} | |
} | |
System.out.println("SERVER: left\nGAME OVER "); | |
scanIn.close(); | |
client.close(); | |
} catch (IOException e) { | |
System.out.println("SERVER: left\nGAME OVER "); | |
// e.printStackTrace(); | |
} | |
} | |
} |
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
// File Name GreetingServer.java | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.net.SocketTimeoutException; | |
import java.util.Scanner; | |
public class GreetingServer extends Thread { | |
private ServerSocket serverSocket; | |
public GreetingServer(int port) throws IOException { | |
serverSocket = new ServerSocket(port); | |
serverSocket.setSoTimeout(1000000); | |
} | |
public void run() { | |
final Scanner scanIn = new Scanner(System.in); | |
while (true) { | |
try { | |
System.out.println("SERVER: Waiting for client on port " + serverSocket.getLocalPort() + "..."); | |
Socket server = serverSocket.accept(); | |
System.out.println("SERVER: " + "Game start"); | |
System.out.println("SERVER: Just connected to " + server.getRemoteSocketAddress()); | |
final DataInputStream in = new DataInputStream(server.getInputStream()); | |
System.out.println("CLIENT: " + in.readUTF()); | |
final DataOutputStream out = new DataOutputStream(server.getOutputStream()); | |
out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\n..Welcome...!"); | |
new Thread() { | |
public void run() { | |
try { | |
while (true) { | |
String msg = in.readUTF(); | |
if(!msg.startsWith(" I received ")) { | |
out.writeUTF(" I received \"" + msg.replace("\"", "") + "\""); | |
} | |
System.out.println("CLIENT: " + msg); | |
} | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
System.out.println("CLIENT closed"); | |
} | |
}; | |
}.start(); | |
System.out.println("Enter message : "); | |
String sWhatever = scanIn.nextLine(); | |
while (!sWhatever.equalsIgnoreCase("q")) { | |
System.out.println("SERVER: " + sWhatever); | |
out.writeUTF("\"" + sWhatever + "\""); | |
System.out.println("Enter message : "); | |
sWhatever = scanIn.nextLine(); | |
} | |
out.writeUTF("q"); | |
server.close(); | |
System.out.println("SERVER: Closed" + "Game over"); | |
} catch (SocketTimeoutException s) { | |
System.out.println("Socket timed out!"); | |
break; | |
} catch (IOException e) { | |
System.out.println("SERVER: Closed" + "Game over"); | |
break; | |
} | |
} | |
scanIn.close(); | |
} | |
public static void main(String[] args) { | |
int port = Integer.parseInt(args[0]); | |
try { | |
Thread t = new GreetingServer(port); | |
t.start(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment