Results 1 to 2 of 2
  1. #1
    andre1011 is offline Member
    Join Date
    Feb 2009
    Location
    Sacramento CA
    Posts
    17
    Rep Power
    0

    Default Connecting to Server Application

    Networking Application Issue is connecting
    Hello I have a client application that can not connect to the server
    To reproduce my problem you will need

    1) Have two computers (one for server and the other for the client)
    2) I got my server IP address from windows => ipconfig /all and the port number
    I made up is 12345
    3)The client is throwing a
    UnknownHostexception: addr is of illegal length
    4)I put in the server IP address and port number as command line arguments for
    the client

    Here is the server code if you need it

    /*
    filename: morseCodeServer.java
    author: Andre Long
    date: March 13, 2009
    purpose: To allow two client applications to communicate via morse code
    */

    import java.io.EOFException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import java.util.Scanner;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    import java.util.concurrent.locks.Condition;
    import java.util.Formatter;

    public class morseCodeServer extends JFrame
    {
    private JTextArea outputArea; //place to output messages
    private ServerSocket server; //server socket to connect with clients
    private ExecutorService runTelegraph;
    private Lock telegraphLock; //lock telegraph until there are two speakers
    private Speakers[] speaker;
    private int numberOfSpeakers = 0;

    public morseCodeServer()
    {
    super("Telegraph Server");
    runTelegraph = Executors.newFixedThreadPool(2); //two threads for two clients
    telegraphLock = new ReentrantLock();
    try
    {
    server = new ServerSocket(12345, 2);
    }
    catch(IOException ioException)
    {
    ioException.printStackTrace();
    System.exit(1);
    }
    outputArea = new JTextArea();
    add(outputArea, BorderLayout.CENTER);
    outputArea.setText("Server waiting for connections \n");
    setSize(400,400);
    setVisible(true);
    speaker = new Speakers[2];

    }//end of morseCodeServer Constructor

    public void execute()
    {
    while(true)
    {
    Speakers speaker;
    numberOfSpeakers++;
    try //wait for two connections start runnable
    {
    speaker = new Speakers(server.accept(), numberOfSpeakers);
    Thread t = new Thread(speaker);
    t.start();
    outputArea.setText("Speaker " + numberOfSpeakers + " connected to server \n");

    }//end try block
    catch(IOException ioException)
    {
    ioException.printStackTrace();
    System.exit(1);
    }//end catch block
    }//end of while loop
    }//end of function execute


    private class Speakers implements Runnable
    {
    private Socket connection; //connects to the speaker
    private Scanner input; //receives input from speaker
    private Formatter output; //sends output to speaker
    private int speakerNumber; //tracks which speaker
    private boolean suspended = true; //tells if thread is suspended

    public Speakers(Socket socket, int number)
    {
    speakerNumber = number;
    connection = socket;

    try //get io streams from socket
    {
    input = new Scanner(connection.getInputStream());
    output = new Formatter(connection.getOutputStream());
    outputArea.setText("Got IO Streams \n");

    }//end try block
    catch(IOException ioException)
    {
    ioException.printStackTrace();
    System.exit(1);

    }//end catch block

    }//end of Speakers Constructor

    public void run()
    {
    String sendToClient;
    String getFromClient;
    output.format("you are Speaker " + numberOfSpeakers + "\n");
    try
    {
    while(true)
    {
    output.flush();
    getFromClient = input.nextLine();
    sendToClient = getFromClient;
    output.format("%s",sendToClient);
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    System.exit(1);

    }
    }//end of function run


    }//end of inner class Speakers

    public static void main(String args[])
    {
    morseCodeServer telegraph = new morseCodeServer();
    telegraph.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
    telegraph.execute();
    }//end of main function

    }// end of class morseCodeServe

    And here is the client code

    /*
    filename: morseCodeClient.java
    author: Andre Long
    date: March 18, 2009
    purpose: Will communicate to another client via a server
    the messages will be translate from English
    into morse code and back again
    */
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.net.Socket;
    import java.net.InetAddress;
    import java.io.IOException;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.JButton;
    import java.util.Formatter;
    import java.util.Scanner;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ExecutorService;



    public class morseCodeClient extends JFrame implements Runnable
    {
    private JTextField idField; //displays which speaker or end user you currently are
    private JPanel mainPanel; //holds display for English and morse code
    private JTextArea displayEnglish; //displays messages in English running back and forth
    private JTextArea displayCode; //displays the English message translated into morse code
    private Socket endUserConnection; //connection to the server
    private Scanner input; //input from server
    private Formatter output; //output to server
    private String serverIPAddressString;
    private String serverPortNumberString;
    private byte[] serverIPAddressBytes;
    private InetAddress serverIPAddressNumber;
    private int serverPortNumber;
    private JButton send;

    public morseCodeClient(String serverIPAddress, String serverPortNumber)
    {
    this.serverIPAddressString = serverIPAddress;
    this.serverPortNumberString = serverPortNumber;
    mainPanel = new JPanel();
    displayEnglish = new JTextArea(10,30);
    displayCode = new JTextArea(2,30);
    mainPanel.add(new JScrollPane(displayCode), BorderLayout.NORTH);
    mainPanel.add(new JScrollPane(displayEnglish), BorderLayout.CENTER);
    add(mainPanel, BorderLayout.CENTER);
    idField = new JTextField();
    idField.setEditable(false);
    idField.setText("waiting to connect to server ...");
    add(idField, BorderLayout.NORTH);
    send = new JButton("send message");
    add(send, BorderLayout.SOUTH);
    setSize(300,225);
    setVisible(true);
    startClient();

    }//end of morseCodeClient Constructor

    public void startClient()
    {
    try
    {
    serverIPAddressBytes = serverIPAddressString.getBytes();
    serverIPAddressNumber = InetAddress.getByAddress(serverIPAddressString, serverIPAddressBytes);
    serverPortNumber = Integer.parseInt(serverPortNumberString);

    endUserConnection = new Socket( serverIPAddressNumber, serverPortNumber );
    input = new Scanner(endUserConnection.getInputStream());
    output = new Formatter(endUserConnection.getOutputStream());
    }//end try
    catch( IOException ioException )
    {
    ioException.printStackTrace();
    }//end catch
    ExecutorService worker = Executors.newFixedThreadPool(1);
    worker.execute(this);
    }//end function startClient

    public void run()
    {

    }//end function run

    public static void main(String args[])
    {
    morseCodeClient endUser;

    if(args.length == 0)
    {
    endUser = new morseCodeClient("127.0.0.1", "12345"); //localhost
    }
    else
    {
    endUser = new morseCodeClient( args[0], args[1] ); //use arguments
    }

    endUser.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);

    }//end of main function

    }//end of class morseCodeClient

    I believe the problem is at
    serverIPAddressNumber = InetAddress.getByAddress(serverIPAddressString, serverIPAddressBytes);

    Any ideas?

  2. #2
    xdarui is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default

    import java.net.InetAddress;

    public class T {
    public static void main(String args[]) throws Exception {
    byte[] bs = new byte[] { (byte) 192, (byte) 168, 0, 1 };
    System.out.println(InetAddress.getByAddress(bs));
    }
    }

Similar Threads

  1. connecting to remote SQL server
    By mahasarathi in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-04-2009, 06:11 AM
  2. Connecting to SQL Server 2008 DB
    By Lyle in forum Eclipse
    Replies: 1
    Last Post: 02-22-2009, 04:26 PM
  3. Applet connecting to server
    By zenMarko in forum Java Applets
    Replies: 1
    Last Post: 11-15-2008, 04:32 PM
  4. Connecting To SQL Server
    By rmaadil in forum JDBC
    Replies: 7
    Last Post: 08-20-2008, 05:53 PM
  5. Connecting to a Web server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •