Question

Write a program that shows the operation of TCP/IP socket. (Java) Please if you can, put...

Write a program that shows the operation of TCP/IP socket. (Java)

Please if you can, put a comment to let me understand the code better.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// A Java program for a Client

//Here we see how the TCp/IP Socket works

/** Here in Socket socket = new Socket(“127.0.0.1”, 5000).

Here the first argument is the IP address of Server. ( 127.0.0.1 is the IP address of localhost, where code will run on single stand-alone machine). Here you should ideally check your ipadress from ipconfig command in command prompt and replace it.

Second argument – TCP Port. (Just a number representing which application to run on a server. For example, HTTP runs on port 80. Port number can be from 0 to 65535)

**/

import java.net.*;

import java.io.*;

  

public class Client

{

    // initialize socket and input output streams

    private Socket socket            = null;

    private DataInputStream input   = null;

    private DataOutputStream out     = null;

  

    // constructor to put ip address and port

    public Client(String address, int port)

    {

        // establish a connection

        try

        {

            socket = new Socket(address, port);

            System.out.println("Connected");

  

            // takes input from terminal

            input = new DataInputStream(System.in);

  

            // sends output to the socket

            out    = new DataOutputStream(socket.getOutputStream());

        }

        catch(UnknownHostException u)

        {

            System.out.println(u);

        }

        catch(IOException i)

        {

            System.out.println(i);

        }

  

        // string to read message from input

        String line = "";

  

        // keep reading until "Over" is input

        while (!line.equals("Over"))

        {

            try

            {

                line = input.readLine();

                out.writeUTF(line);

            }

            catch(IOException i)

            {

                System.out.println(i);

            }

        }

  

        // close the connection

        try

        {

            input.close();

            out.close();

            socket.close();

        }

        catch(IOException i)

        {

            System.out.println(i);

        }

    }

  

    public static void main(String args[])

    {

//Do not forget to replace Ip and Port in the next line

Client client = new Client("127.0.0.1", 5000);

    }

}

Add a comment
Know the answer?
Add Answer to:
Write a program that shows the operation of TCP/IP socket. (Java) Please if you can, put...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT