This is the Simple program to open the socket.
keep in mind that Socket is the combination of IP:Port Number
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package server;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
class Server{
public static void main(String args[]){
ServerSocket ss=null;
OutputStream os=null;
String str="Hello Saeed Ahmad How are you";
byte b[];
try{
ss=new ServerSocket(49160);
System.out.println("Waiting for a Client");
Socket cli=ss.accept();
System.out.println("Connected---"+cli);
os=cli.getOutputStream();
b=str.getBytes();
os.write(b);
os.flush();
}catch(Exception e){System.out.println("Port Opens are "+e);}
}//End of main
}//End of Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package client;
import java.net.*;
import java.io.*;
class Client{
public static void main(String args[]){
int i=0;
Socket s=null;
char ch;
String s1=null;
InputStream is=null;
try{
s=new Socket("127.0.0.1",49160);
Thread.sleep(1000);
is=s.getInputStream();
do{
i=is.read();
ch= (char) i;
s1+=ch;
}while(i!=-1);
System.out.println(s1);
}catch(Exception e){System.out.println("Port Opens are :"+e.getMessage());}
}//End of main
}//End of Class
keep in mind that Socket is the combination of IP:Port Number
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package server;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
class Server{
public static void main(String args[]){
ServerSocket ss=null;
OutputStream os=null;
String str="Hello Saeed Ahmad How are you";
byte b[];
try{
ss=new ServerSocket(49160);
System.out.println("Waiting for a Client");
Socket cli=ss.accept();
System.out.println("Connected---"+cli);
os=cli.getOutputStream();
b=str.getBytes();
os.write(b);
os.flush();
}catch(Exception e){System.out.println("Port Opens are "+e);}
}//End of main
}//End of Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package client;
import java.net.*;
import java.io.*;
class Client{
public static void main(String args[]){
int i=0;
Socket s=null;
char ch;
String s1=null;
InputStream is=null;
try{
s=new Socket("127.0.0.1",49160);
Thread.sleep(1000);
is=s.getInputStream();
do{
i=is.read();
ch= (char) i;
s1+=ch;
}while(i!=-1);
System.out.println(s1);
}catch(Exception e){System.out.println("Port Opens are :"+e.getMessage());}
}//End of main
}//End of Class
No comments:
Post a Comment