DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Socket Communication in Client/Server Relationship

Socket Communication in Client/Server Relationship

Joseph Randolph user avatar by
Joseph Randolph
·
May. 21, 10 · Java Zone · Interview
Like (0)
Save
Tweet
16.78K Views

Join the DZone community and get the full member experience.

Join For Free
When writing java code for a Client or Server, using Sockets are the most efficient way to communicate between the client and server.  Sockets are flexible, sufficient, easy to implement, and they do not use a lot of network traffic.

There are many reasons why your client/server may not be connecting to each other:

  • Make sure you import “java.net”
  • Make sure the socket is bound to a port number
  • Make sure you have the correct IP address
  • Make sure the socket for the server accepts the right port number


In Java, the client initiates the connection by instantiating a Socket.  The Socket constructor usually takes in two parameters: TCP/IP address of the server and port number of the application.  After the client instantiates the Socket, the client will instantiate DataInputStream and DataOutputStream.  These high-level classes share the same Socket.  The client will wait for the server to accept the connection.

How to code this in Java:

Socket s = new Socket(serverAddress, 1234); //connect to server app
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(question);
String answer = dis.readUTF(); //wait for reply
The Server application uses a “ServerSocket” class and the constructor takes the port number as a parameter.  If you are an application programmer on the server, you would need to get a port number from the administrator.  When you call accept method, the server application waits for the client to call the port number.  After the ServerSocket accepts the client, it negotiates with the client to switch to an available port so that the main port is still open for other clients to connect to the server.  The server will wait for the client to say something and each client will spend its time waiting in a readUTF method.

How to code this in Java:
Socket s = ss.accept(); // wait for a client connection
DataInputStream dis = new DataInputStream (s.getInputStream());
DataOutputStream dos = new DataOutputStream (s.getOutputStream());
String question = dis.readUTF();
application Java (programming language) Connection (dance) Programmer (hardware) Network

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Making Machine Learning More Accessible for Application Developers
  • Migrating Secrets Using HashiCorp Vault and Safe CLI
  • Debugging Deadlocks and Race Conditions
  • How to Determine if Microservices Architecture Is Right for Your Business?

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo