I am trying to convert these programs to accommodate wireless networks as well as wired.
This Java Chat code works well on wired LAN but not on wireless. Why? Port and IP address are the same on the client and server. What are the differences in connectivity between wired and wireless IP addresses and wired? What changes are necessary?
This is the Server:
import java.io.*;
import java.net.*;
import java.util.*;
public class servTest
{
public static final int PORT = 9000;
public static int timer = 20000; //Arbitrary timer
public static void main(String[] args) throws IOException
{
ServerSocket s= new ServerSocket(PORT);
InetAddress []addr = InetAddress.getLocalHost();
String str = "";
boolean inOpen = true,outOpen = true;
Socket socket = new Socket();
do
{
String thisUser="";
try
{
for(InetAddress a:addr)
System.out.println("\n\n\n Server address "
+a.toString());
BufferedReader in;
PrintWriter out;
while(timer > 0)
{
timer--;
socket = s.accept();
in = new BufferedReader (
new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(new BufferedWriter
(new OutputStreamWriter(
socket.getOutputStream())),
true);
// input from stream **********************
try
{
in.ready();
str = in.readLine(); //get network
System.out.println(str); //print packet
inOpen = true;
}
catch(Exception e)
{
inOpen = false;
}
try
{
out.println("SERVER timer" + timer);
outOpen = true;
}
catch(Exception e)
{
outOpen = false;
System.out.println("Exception "+e +
" output not open");
}
} // END network loop
}
finally
{
// close stream **********************
if (inOpen || outOpen)
{
inOpen = false;
outOpen = false;
socket.close();
}
s.close();
String message = "Socket closed.\n\n";
}
}
while (true); // infinite loop for test
}
}
This is the Client:
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
//import java.util.Random;
public class client
{
public static void main(String[] args) throws IOException
{
InetAddress localAddr = InetAddress.getLocalHost();
String ServerAddress = "";
ServerAddress = JOptionPane.showInputDialog(
"Enter server ip address.","127.0.0.1");
int PORT = 9000; //Must be the same as the server.
InetAddress addr =
InetAddress.getByName(ServerAddress);
Socket socket;
BufferedReader netIn;//Use BufferedReader for input from
//network
PrintWriter netOut; //Use PrintWriter for output to the
//network.
Scanner myIn = new Scanner(System.in);
String sentence = "";
boolean inOpen = true,outOpen = true;
int x = 0;
String str = "";
while (true) //infinite loop to test
{
pause(1);
socket = new Socket(addr,PORT);
outOpen = true;
netIn = new BufferedReader ( //attach netIn to input
new InputStreamReader( //from the network
socket.getInputStream()));//(inputStream)
netOut = new PrintWriter(new BufferedWriter//Attach netOut to
(new OutputStreamWriter( //output to the
socket.getOutputStream())), //network
true);
x++;
// output to stream **********************
System.out.print("..............type.......................");
sentence = myIn.nextLine();
try
{
netOut.println("CLIENT ****** " // output to network
+x+" "+localAddr + ": " + sentence
);
}
catch(Exception e)
{
outOpen = false;
System.out.println("Exception "+e + " output not open");
socket.close();
}
// input from stream **********************
inOpen = true;
try
{
netIn.ready();
str = netIn.readLine(); //get network info
if(str.equals("END")) break;
System.out.println( //print packet on screen
"Server echoing input "+str);
}
catch(Exception e)
{
System.out.println("Exception "+e + " input not open" + x);
inOpen = false;
socket.close();
}
}
}
public static void pause (long r)
{
try
{
Thread.sleep(r);
}
catch (Exception e)
{
out.println(" sleep error " + e);
}
}
}