create two file one for client and one for server
for client you will have to code like this
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jsocket;
/**
*
* @author jawadmahmood
*/
import java.net.*;
import java.io.*;
import javax.swing.*;
public class JSocket {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
Socket s = new Socket("localhost", 2222);
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
String msg = JOptionPane.showInputDialog("Enter your name");
pw.println(msg);
msg = br.readLine();
JOptionPane.showMessageDialog(null, msg);
s.close();
}
catch(Exception ex){
System.out.println("The error is " + ex);
}
}
}
for client you will have to code like this
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jsocket;
/**
*
* @author jawadmahmood
*/
import java.net.*;
import java.io.*;
import javax.swing.*;
public class JSocket {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
Socket s = new Socket("localhost", 2222);
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
String msg = JOptionPane.showInputDialog("Enter your name");
pw.println(msg);
msg = br.readLine();
JOptionPane.showMessageDialog(null, msg);
s.close();
}
catch(Exception ex){
System.out.println("The error is " + ex);
}
}
}
then create the server socket like this
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package echoserver;
/**
*
* @author jawadmahmood
*/
import java.net.*;
import java.io.*;
import javax.swing.*;
public class EchoServer {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
ServerSocket ss = new ServerSocket(2222);
System.out.println("Server started");
while(true){
Socket s = ss.accept();
System.out.println("Connection request received" );
InputStream is =s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
String name = br.readLine();
String msg = "Hello "+name+ "from server";
pw.println(msg);
s.close();
}
}catch(Exception ex){
System.out.println(ex);
}
}
}
when you start the program it will open the input dialog box when you enter the name or something else it will return the text with hello in dialog box .
as shown in the pic
No comments:
Post a Comment