* To communicate between two computers you will need to allocate one computer as Server, the other as Client.
* You will need to know the address of the machines you are connecting to. Run the command prompt and use the ipconfig command to find this out.
* Set the server running, and then run the client. It should connect and send a message.
# MyServer
```java
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) {
try {
// Note I have used 999 as a port number.
// Make sure the your client does the same.
// Use ipconfig to find the address of the machine the server is running on.
ServerSocket listener = new ServerSocket(999);
System.out.println("Waiting to accept client...");
Socket client = listener.accept();
System.out.println("Client accepted...");
// a lot of code to get buffered input and output streams
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
PrintWriter pout = new PrintWriter(out, true);
// read and print a message from the client
String s = bin.readLine();
System.out.println(s);
// send a message to the client
pout.println("This is the server speaking!");
} catch (IOException e) {
System.err.println(e);
}
}
}
}
```
# MyClient
```java
package myclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class MyClient {
public static void main(String[] args) {
try {
// Make sure server is running first
// use ipconfig to find server address
// check the port number on the server program
System.out.println("Attempting to connect to server...");
Socket sock = new Socket("address", 999);
System.out.println("Connected to Server...");
// a lot of code to get buffered input and output streams
InputStream in = sock.getInputStream();
OutputStream out = sock.getOutputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
PrintWriter pout = new PrintWriter(out, true);
// send a message to the server
pout.println("Can you hear me?");
// read a message sent from the server
String s = bin.readLine();
System.out.println(s);
} catch (UnknownHostException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
}
```
# Sending and Receiving
You can paste the following code into both client and server to allow sending and receiving of text. There is problem with this method, however: blocking. Basically, the program waits (or blocks) until a line is read, before it moves on. Try running the program to see what this means.
```java
Scanner scan = new Scanner(System.in);
String keyInput;
do
{
//read a message from the keyboard and send it
keyInput= scan.nextLine();
pout.println(keyInput);
//wait for a message from the other end, and print it
System.out.println(bin.readLine());
}while(!keyInput.equalsIgnoreCase("end"));
```
The solution to the above problem is to use [[Threads]].