socket programming in java
Java
$ java GreetingServer 6066
Waiting for client on port 6066...
// File Name GreetingServer.java
import java.net.*;
import java.io.*;
public class GreetingServer extends Thread {
private ServerSocket serverSocket;
public GreetingServer(int port) throws IOException {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
}
public void run() {
while(true) {
try {
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to " + server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
+ "\nGoodbye!");
server.close();
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
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();
}
}
}// File Name GreetingClient.java
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();
}
}
}
Also in Java:
- Title
- javafx textarea size
- Category
- Java
- Title
- how to add a keylistener to a jframe
- Category
- Java
- Title
- java cannot find file path
- Category
- Java
- Title
- change number into array in java
- Category
- Java
- Title
- boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- Category
- Java
- Title
- how to make a dictionary in java
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- http client java
- Category
- Java
- Title
- java file download
- Category
- Java
- Title
- java linked list iterator
- Category
- Java
- Title
- sieve in java
- Category
- Java
- Title
- java check if int
- Category
- Java
- Title
- how to create a constructor in java
- Category
- Java
- Title
- transformer un string en Biginteger java
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- java read lines from file
- Category
- Java
- Title
- color from hex code flutter
- Category
- Java
- Title
- isnumber java
- Category
- Java
- Title
- string method example in java
- Category
- Java
- Title
- check if string is null or empty java
- Category
- Java
- Title
- java self signed certificate truststore
- Category
- Java
- Title
- try block in java
- Category
- Java
- Title
- catch array out of bounds exception java
- Category
- Java
- Title
- how take inputs in java with message
- Category
- Java
- Title
- using class in java
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- processing sound android
- Category
- Java
- Title
- how to change the icon of a jframe
- Category
- Java
- Title
- java hashmap syntax
- Category
- Java
- Title
- how do you make a method that returns an array java
- Category
- Java
- Title
- inheritance in java
- Category
- Java
- Title
- how to format a double in java to 2 decimal places
- Category
- Java
- Title
- parseints(str) java
- Category
- Java
- Title
- java get icon for file type
- Category
- Java
- Title
- arraylist array
- Category
- Java
- Title
- how to read file from assets folder in android
- Category
- Java
- Title
- java getter
- Category
- Java
- Title
- iterate through an arraylist java
- Category
- Java
- Title
- place.getlatlng() returning null
- Category
- Java
- Title
- java packages example
- Category
- Java
- Title
- ndroid.support.v4.content.FileProvider
- Category
- Java
- Title
- socket programming in java
- Category
- Java
- Title
- trim() and split() in java
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- iterate through hashtable java
- Category
- Java
- Title
- writing to a text file java
- Category
- Java
- Title
- how to get the dimensions of a 2d array in java
- Category
- Java
- Title
- set preference value android
- Category
- Java
- Title
- how to loop trough an object java script
- Category
- Java
- Title
- java split string
- Category
- Java
- Title
- java final meaning
- Category
- Java
- Title
- int to binary java
- Category
- Java
- Title
- java not instanceof
- Category
- Java
- Title
- bracket balancing program in java
- Category
- Java
- Title
- how make a final variable in java
- Category
- Java
- Title
- calculating the percentile in java
- Category
- Java
- Title
- spigot title
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- java hashmap size
- Category
- Java
- Title
- java how to print a newline
- Category
- Java
- Title
- java new string array
- Category
- Java
- Title
- sign flutter app android studio
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- how to open a webpage with java
- Category
- Java
- Title
- convert string to mayus java
- Category
- Java
- Title
- how to run java program in linux server
- Category
- Java
- Title
- else statement java
- Category
- Java
- Title
- java how to get all threads
- Category
- Java
- Title
- java map declaration
- Category
- Java
- Title
- java setter
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- counting repeated characters in a string in java
- Category
- Java
- Title
- output statement java
- Category
- Java
- Title
- array rotation program in java
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- java stream find specific element
- Category
- Java
- Title
- recursion in java
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- how to check if rs next is null
- Category
- Java
- Title
- java length of matrix
- Category
- Java
- Title
- javafx detect collision
- Category
- Java
- Title
- how to have a only number type in java
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- print in java
- Category
- Java
- Title
- change brightness of image in java
- Category
- Java
- Title
- java list to set
- Category
- Java
- Title
- random java
- Category
- Java
- Title
- java set to list
- Category
- Java
- Title
- eliminar fila jtable java
- Category
- Java
- Title
- convert int array to integer list java
- Category
- Java
- Title
- java store hexadecimal value
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- partial view tiles spring
- Category
- Java
- Title
- Character.getNumericValue['z']
- Category
- Java
- Title
- java instantiate collection with values
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- math maximum java
- Category
- Java
- Title
- reverse number in java
- Category
- Java
- Title
- declare bufferedreader java
- Category
- Java
- Title
- how to get binary value in java
- Category
- Java