Hello
I had an experience by Server/Client and I understood it is too hard.
The code below is my experience. If you use this code in one station by two cmd window the code works very well. If you use it in two separate stations, your LAN and the windows settings should be OK. In my experience, client code couldn't find the server in some of the stations in my office!
good lock
server code:
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class TalkServer {
private static Scanner ScreenInput;
private static ServerSocket SServer;
private static Scanner socketIN;
public static void main(String[] args)
{
System.out.println("TalkServer Version 1.0");
System.out.print("Type the port: ");
ScreenInput = new Scanner(System.in);
String sPort=ScreenInput.nextLine();
int iPort=Integer.parseInt(sPort);
System.out.println("Port Nomber is:"+iPort);
System.out.println("Wait for a client...");
try
{
SServer = new ServerSocket(iPort);
Socket SClient;
SClient=SServer.accept();
String client;
client = SClient.getInetAddress().toString();
System.out.println("Connected to " + client);
socketIN = new Scanner(SClient.getInputStream());
PrintWriter SocketOut;
SocketOut=new PrintWriter(SClient.getOutputStream(),true);
while (true)
{
System.out.print("Listening for Client:");
String sInput=socketIN.nextLine();
System.out.println(sInput);
SocketOut.println("Server:"+sInput);
if (sInput.equalsIgnoreCase("bye"))
break;
}
SClient.close();
SServer.close();
System.out.println("Server closed");
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Error in connection");
}
}
}
Client code:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class TalkClient {
private static Scanner keyBoard;
private static Scanner socketIN;
public static void main(String[] args)
{
System.out.println("TalkClient Version 1.0");
System.out.print("Type the port:");
keyBoard = new Scanner(System.in);
String sPort=keyBoard.nextLine();
int iPort=Integer.parseInt(sPort);
System.out.println("Port Nomber is "+iPort);
System.out.print("Type the Host:");
String sHost=keyBoard.nextLine();
Socket s;
InetAddress ip;
try
{
ip=InetAddress.getByName(sHost);
System.out.println("This Host has The IPs below:");
InetAddress[] address=InetAddress.getAllByName(sHost);
for (InetAddress iIP: address)
System.out.println(iIP.toString());
System.out.println("Now Connecting to the Server...");
s=new Socket(ip,iPort);
System.out.println("Connected to "+iPort);
socketIN = new Scanner(s.getInputStream());
PrintWriter SocketOut;
SocketOut=new PrintWriter(s.getOutputStream(),true);
while(true)
{
System.out.print("Say something:");
String sSay=keyBoard.nextLine();
SocketOut.println(sSay);
String sListen=socketIN.nextLine();
System.out.println("check back:"+sListen);
if (sSay.equalsIgnoreCase("bye")) break;
}
}
catch (UnknownHostException e)
{
System.out.println("the hos t is unknown");
}
catch (IOException e)
{
System.out.println("Netwerk Error");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}