Results 1 to 4 of 4

Thread: Java sockets

  1. #1
    Camzie is offline Member
    Join Date
    Nov 2009
    Location
    Pretoria
    Posts
    12
    Rep Power
    0

    Default Java sockets

    Hi there

    I need help with java sokets!

    I want to know if my code is correct
    //
    //
    //
    //
    //
    Here is my code for the Client Socket...

    public class TCPClient
    {
    public static void main(String[] argv) throws Exception
    {
    String messageFromClient = null;
    String messageFromServer = null;

    Socket clientSocket = new Socket("localhost", 0000);
    System.out.println("Enter in Client: ");

    //code needed for keyboard input
    //getting user input
    BufferedReader inputFromUserOne = new BufferedReader(new InputStreamReader(System.in));
    inputFromUserOne.readLine();

    //reading the message from user
    messageFromClient = inputFromUserOne.readLine();

    //sending user input to server
    DataOutputStream outToServerOne = new DataOutputStream(clientSocket.getOutputStream());
    outToServerOne.writeBytes(messageFromServer);

    //getting message back from server
    BufferedReader inFromServerOne = new BufferedReader(new InputStreamReader(System.in));
    inFromServerOne.readLine();

    outToServerOne.writeBytes(messageFromClient + '\n');
    messageFromServer = inFromServerOne.readLine();

    System.out.println("Welcome " + inputFromUserOne);

    ///////////////////////////////////////////////////////////////////////////////
    System.out.println("Enter in Host: ");

    //code needed for keyboard input
    //getting user input
    BufferedReader inputFromUserTwo = new BufferedReader(new InputStreamReader(System.in));
    inputFromUserTwo.readLine();

    //reading the message from user
    messageFromClient = inputFromUserOne.readLine();

    //sending user input to server
    DataOutputStream outToServerTwo = new DataOutputStream(clientSocket.getOutputStream());
    outToServerTwo.writeBytes(messageFromServer);

    //getting message back from server
    BufferedReader inFromServerTwo = new BufferedReader(new InputStreamReader(System.in));
    inFromServerTwo.readLine();

    outToServerOne.writeBytes(messageFromClient + '\n');
    messageFromServer = inFromServerTwo.readLine();

    System.out.println("Host: " + inputFromUserTwo);

    ///////////////////////////////////////////////////////////////////////////////
    System.out.println("Enter in Mac Address: ");

    //code needed for keyboard input
    //getting user input
    BufferedReader inputFromUserThree = new BufferedReader(new InputStreamReader(System.in));
    inputFromUserThree.readLine();

    //reading the message from user
    messageFromClient = inputFromUserOne.readLine();

    //sending user input to server
    DataOutputStream outToServerThree = new DataOutputStream(clientSocket.getOutputStream());
    outToServerThree.writeBytes(messageFromServer);

    //getting message back from server
    BufferedReader inFromServerThree = new BufferedReader(new InputStreamReader(System.in));
    inFromServerThree.readLine();

    outToServerOne.writeBytes(messageFromClient + '\n');
    messageFromServer = inFromServerThree.readLine();

    System.out.println("Mac Address: " + inputFromUserThree);

    System.out.println("From Server: " + messageFromServer);
    clientSocket.close();
    }
    }
    //
    //
    //
    //
    //
    Here is my code for the Server Socket...


    public class TCPServer
    {
    public static void main(String [] args) throws IOException
    {
    String clientMessage;
    String serverMessage;
    ServerSocket welcomeSocket = new ServerSocket(1234);

    while (true)
    {
    Socket connectionSocket = welcomeSocket.accept();
    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream( )));

    DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream( ));

    clientMessage = inFromClient.readLine();

    serverMessage = inFromClient.readLine() + '\n';

    outToClient.writeBytes(serverMessage);
    }
    }
    }
    //
    //
    //
    //
    //
    Here is my code for the Connection Socket...

    public class TCPConnection
    {
    Socket connection = null;
    String address = "198.168.199.162";

    public static void main(String[] args) throws Exception
    {
    int serverPort = 0;
    ServerSocket ss = new ServerSocket(serverPort);
    Socket socket = new Socket();
    socket = ss.accept();
    }

    private ObjectOutputStream out;
    private ObjectInputStream in;
    String message;

    void run() throws IOException
    {
    ServerSocket providerSocket = null;
    try
    {
    //creating a server socket
    providerSocket = new ServerSocket();

    //wait for connection
    System.out.println("Waiting for connection");
    connection = providerSocket.accept();
    System.out.println("Connection received from " + connection.getInetAddress().getHostName());

    //get input and output streams
    out = new ObjectOutputStream(connection.getOutputStream());
    out.flush();
    in = new ObjectInputStream(connection.getInputStream());
    sendMessage("Connection successful");

    //the two parts communicate via the input and output streams

    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }
    finally
    {
    //Closing connection
    try
    {
    in.close();
    out.close();
    providerSocket.close();
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }
    }
    }

    public void sendMessage(String string) throws IOException
    {
    try
    {
    out.writeObject(message);
    out.flush();
    System.out.println("Server: " + message);
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }

    }
    }
    //
    //
    //
    //
    //
    If someone could please help me
    I'll appreciate it

    Thanx
    Camzie
    Last edited by Camzie; 12-18-2009 at 01:21 PM.

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    First of all use code tags to paste your code, or else it is not at all readable. I guess you are the best person to judge whether the code is correct or not by executing it.
    Swastik

  3. #3
    Camzie is offline Member
    Join Date
    Nov 2009
    Location
    Pretoria
    Posts
    12
    Rep Power
    0

    Default

    Thanx alot
    Camzie

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes compile your code and run it. Then see what happen. If you comes with any error in your code, then read the error message carefully. Seems you are using NetBeans, and it display the error in nicer way(like most of other IDEs do).

Similar Threads

  1. Sockets NIO
    By aamp in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 10:56 AM
  2. Java Server & C# client communicating through sockets
    By yukijocelyn in forum Networking
    Replies: 0
    Last Post: 09-05-2008, 10:05 PM
  3. Java application using sockets!
    By rameshraj in forum Threads and Synchronization
    Replies: 1
    Last Post: 06-11-2008, 06:13 PM
  4. Sockets
    By Zosden in forum Networking
    Replies: 16
    Last Post: 05-27-2008, 04:55 PM
  5. Help with Sockets
    By Eric in forum Networking
    Replies: 3
    Last Post: 12-01-2007, 08:09 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
  •