Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java socket programming

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java socket programming

    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


  2. #2
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: Java socket programming

    Quote Originally Posted by lucy View Post
    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.

Similar Threads

  1. Is Java Programming Best?
    By messenger in forum Java IDEs
    Replies: 3
    Last Post: May 4th, 2010, 09:11 PM
  2. Please help me with this Java programming
    By simply_stunning79 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 12th, 2010, 07:04 AM
  3. Please help me with this Java programming
    By simply_stunning79 in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 22nd, 2010, 06:48 PM
  4. Towards java programming...
    By emigrant in forum Member Introductions
    Replies: 1
    Last Post: February 15th, 2010, 02:17 AM
  5. Please help me with this Java programming
    By simply_stunning79 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 14th, 2010, 05:42 AM