Created
September 14, 2015 13:03
-
-
Save 18213020-18213038/f9f8875477ecf045b40d to your computer and use it in GitHub Desktop.
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
import java.net.*; | |
import java.io.*; | |
public class GreetingClient | |
{ | |
public static void main(String [] args) | |
{ | |
String serverName = args[0]; | |
int port = Integer.parseInt(args[1]); | |
try | |
{ | |
System.out.println("Connecting to " + serverName + | |
" on port " + port); | |
Socket client = new Socket(serverName, port); | |
System.out.println("Just connected to " | |
+ client.getRemoteSocketAddress()); | |
OutputStream outToServer = client.getOutputStream(); | |
DataOutputStream out = new DataOutputStream(outToServer); | |
out.writeUTF("Hello from " | |
+ client.getLocalSocketAddress()); | |
InputStream inFromServer = client.getInputStream(); | |
DataInputStream in = | |
new DataInputStream(inFromServer); | |
System.out.println("Server says " + in.readUTF()); | |
client.close(); | |
}catch(IOException e) | |
{ | |
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
import java.io.*; | |
import java.net.*; | |
import java.util.Scanner; | |
class GreetingServer{ | |
private static GreetingServer server; | |
static int Port = 3535; | |
static ServerSocket Socket; | |
public GreetingServer(){ | |
try{ | |
Socket = new ServerSocket(setSocket()); | |
}catch(Exception e){ | |
System.out.println("Error: "+e.getMessage()); | |
} | |
} | |
private int setSocket(){ | |
Scanner in = new Scanner(System.in); | |
System.out.print("Enter socket number:"); | |
Port = in.nextInt(); | |
System.out.println("Socket number "+Port+" created."); | |
System.out.println("Listening..."); | |
return Port; | |
} | |
public static void main(String argv[]) throws Exception { | |
server = new GreetingServer(); | |
String clientSentence; | |
String capitalizedSentence; | |
while(true){ | |
Socket connectionSocket = Socket.accept(); | |
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); | |
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); | |
clientSentence = inFromClient.readLine(); | |
System.out.println("Received: " + clientSentence); | |
capitalizedSentence = clientSentence.toUpperCase() + " from "+Port+'\n'; | |
outToClient.writeBytes(capitalizedSentence); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment