Originally Posted by
lucy
hello all,
please could any one help me to get the problem at my code ...
i am just trying to let the server an acknowledegment msg to the client only if the client send him a join msg in this format ......JoinMessage: clientAddress
the problem is that there is an exception in the line" outToCleint.writeBytes" called socket write error
but i do not know how to solve it
here is the code :
* server side*
import java.io.*;
import java.net.*;
import java.util.*;
class Server2 extends Thread{
static protected Set activeClients = new HashSet();
String requestedMessageLine;
String clientAddress;
BufferedReader inFromClient;
DataOutputStream outToClient;
static ServerSocket listener;
static boolean flag;
Socket incoming;
int id;
public static void main(String[] args) throws Exception {
int i = 1;
Socket incoming;
flag = true;
ServerSocket listener = new ServerSocket(8000);
System.out.println(" ------------- Server ON ---------------");
while(true){
System.out.println ("Waiting for connection....");
try{
incoming = listener.accept(); }
catch(SocketException ex){
if(flag){
System.out.println (" what is the msg " + ex.getMessage());
listener = new ServerSocket(8000);
}
return;
}
System.out.println ("Connection accepted from "+ incoming.getInetAddress().getHostName());
Server2 newClient = new Server2(incoming, i++);
activeClients.add(newClient);
newClient.start();
}
}
public Server2(Socket incoming,int id)
{
this.incoming = incoming;
this.id = id;
}
public void run () {
try {
inFromClient = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
outToClient = new DataOutputStream(new DataOutputStream(incoming.getOutputStream()));
requestedMessageLine = inFromClient.readLine();
System.out.println ("Message Recieved is "+ requestedMessageLine);
String[] input=requestedMessageLine.split(" ");
if(input[0].equals("JoinMessage:"))
{
clientAddress = input[1];
System.out.println( " IN SERVER: The Client Access Point Address is : " + clientAddress);
try{
outToClient.writeBytes(" ----- FROM SERVER : Join Message has arrived with client Mac Address : "
+ clientAddress );
}
catch(Exception e){
System.out.println("exception thereeeee " + e);
}
}
if(input[0].equalsIgnoreCase("END") || input[0].equalsIgnoreCase("BYE"))
{
incoming.close();
inFromClient.close();
outToClient.close();
return;
}
inFromClient.close();
outToClient.close();
incoming.close();
Server2.activeClients.remove(this);
}
catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
** Client side **
import java.io.*;
import java.net.*;
class Client2 {
public static void main (String [] args) throws Exception
{
while (true){
try{
BufferedReader inFromUser = new BufferedReader (new InputStreamReader (System.in));
Socket clientSocket = new Socket ( "localhost" , 8000);
System.out.println("client has intiated the connection to the server \r\n ");
System.out.println("To END THE CONNECTION , Wirte : Please end the connection \r\n");
System.out.println("TO SEND A JOIN MESSAGE , Write it in ths Format: \r\n");
System.out.println("JoinMessage: ur address * address of access point u want to join \r\n ");
String clientRequest = inFromUser.readLine();
System.out.println ("Attempting to connect....");
System.out.println ("Connected");
DataOutputStream outToServer = new DataOutputStream (clientSocket.getOutputStream());
DataInputStream inFromServer = new DataInputStream (clientSocket.getInputStream());
outToServer.writeBytes(clientRequest);
System.out.println ("Request Sent ...");
if(clientRequest.equalsIgnoreCase("BYE")||clientRe quest.equalsIgnoreCase("END")){
outToServer.close();
inFromServer.close();
clientSocket.close();
System.out.println ("Closing connections");
return;
}
String result = inFromServer.readLine();
System.out.println("informServer " + result);
System.out.println ("Closing Connection");
outToServer.close();
inFromServer.close();
clientSocket.close();
}
catch(SocketException ex){
System.out.println ("Connection was reset....");
}
catch(UnknownHostException host){
System.out.println("ERROR:- invalid host name: " + host.getMessage());
}
}}
}
Hope somebody could help me
Thanks in advance
hi
i didnt get any prob with that.
this is my ouput:
To END THE CONNECTION , Wirte : Please end the connection
TO SEND A JOIN MESSAGE , Write it in ths Format:
JoinMessage: ur address * address of access point u want to join
JoinMessage: clientAddress
Attempting to connect....
Connected
Request Sent ...
but in the server side, it didnt acknoledge the connection.