How do you write a socket program in Java?
Category:
technology and computing
programming languages
Example of Java Socket Programming (Read-Write both side)
- import java.net.*;
- import java.io.*;
- class MyServer{
- public static void main(String args[])throws Exception{
- ServerSocket ss=new ServerSocket(3333);
- Socket s=ss.accept();
- DataInputStream din=new DataInputStream(s.getInputStream());
Also question is, what is socket in Java with example?
A socket in Java is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.
Herein, how do you program a socket?
Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server.
Three types of sockets are supported:
- Stream sockets allow processes to communicate using TCP. A stream socket provides bidirectional, reliable, sequenced, and unduplicated flow of data with no record boundaries.
- Datagram sockets allow processes to use UDP to communicate.
- Raw sockets provide access to ICMP.