Professional Documents
Culture Documents
1
Main points:
1- meaning of Socket .
2- Client and server socket.
3- Methods and class of client and server socket.
4- Programming example.
2
Meaning of 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 in order to form a connection.
3
Socket in Java:
A socket in Java is one endpoint of a two-way communication
link between two programs running on the network.
package serverpre;
import java.net.*;
import java.io.*; while (!line.equals("Over"))
/** {
* try
{
* @author zaina
line = in.readUTF();
*/ System.out.println(line);
public class serverpre
{ }
catch(IOException i)
private Socket socket = null; {
private ServerSocket server = null; System.out.println(i);
private DataInputStream in = null; }
// constructor with port }
public serverpre(int port) System.out.println("Closing connection");
{
socket.close();
in.close();
try{ }
server = new ServerSocket(port); catch(IOException i){
System.out.println("Server started"); System.out.println(i);
System.out.println("Waiting for a client ..."); }
socket = server.accept(); }
System.out.println("Client accepted"); public static void main(String args[]){
// takes input from the client socket serverpre server = new serverpre(5000);
in = new DataInputStream( }
new }
BufferedInputStream(socket.getInputStream()
)); 8
String line = "";
- First we run the server side, and this
the output:
9
References:
10