Writing Socket Program in Java

Let us consider a client and ServerSocket, so that we can create a sender and listener. Here is the client code:


import java.net.*;
import java.io.*;

public class Client {

public static void main(String[] args) throws IOException{

Socket cls=null;
BufferedReader br=null;
try{
cls= new Socket(InetAddress.getLocalHost(),195);
br = new BufferedReader(new InputStreamReader(cls.getInputStream()));
}
catch (UnknownHostException uh){
System.out.println("host unknown");
System.exit(0);
}
String inp,si;
while((inp=br.readLine())!=null)
{
System.out.println(inp);
}
br.close();
cls.close();
}
}

and this is how you can write a server socket

import java.net.*;
import java.io.*;

public class Servsoc {
public static void main (String args[]) throws IOException{
ServerSocket ss=null;
try{
ss = new ServerSocket(195);
}
catch (IOException ioe)
{
System.out.println("error in finding port 95");
System.exit(1);
}
Socket soc = null;
soc = ss.accept();
System.out.println("connection accepted at : " + soc);
DataOutputStream out = new DataOutputStream(soc.getOutputStream());
String si, clstr;
BufferedReader kybd = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Ready to send message, type exit to end session");
while(!(si =kybd.readLine()).equals("exit")){
int ln = si.length();
for (int i=0; i<ln;i++){
out.write(13);
out.write(10);
out.write(12);
out.flush();
}
out.close();
kybd.close();
ss.close();

}
}
}

Run Server first and then Client and see that the message you typed is getting transferred.

Comments

  1. Selenium training in Chennai
    We provide best selenium training in Chennai with real time scenarios.For real time training reach us 8122241286 and become experts in selenium.
    selenium training in chennai

    ReplyDelete


  2. The biggest change in Selenium recently has been the inclusion of the WebDriver API. Driving a browser natively as a user would either locally or on a remote machine using the Selenium Server it marks a leap forward in terms of browser automation.

    Selenium WebDriver fits in the same role as RC did, and has incorporated the original 1.x bindings. It refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just "WebDriver" or sometimes as Selenium 2.
    Selenium Training Institute in Chennai | Selenium FAQs

    ReplyDelete
  3. I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
    Data science Course Training in Chennai | No.1 Data Science Training in Chennai
    RPA Course Training in Chennai | No.1 RPA Training in Chennai

    ReplyDelete
  4. Vint Ceramic Art | TITNIA & TECHNOLOGY
    Explore an titanium metal trim all new “Vint 1xbet app Ceramic Art” project on apr casino TITNIA & TECHNOLOGY. Our team of kadangpintar sculptors and artists have created new and

    ReplyDelete

Post a Comment

Popular Posts