SocketClient.java work as Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
import java.io.*; | |
import java.util.*; | |
public class SocketClient | |
{ | |
public static void main(String args[]) throws IOException | |
{ | |
Socket s1 = new Socket("localhost",4444); | |
while(true) | |
{ | |
OutputStream s1out = s1.getOutputStream(); | |
DataOutputStream dos = new DataOutputStream (s1out); | |
System.out.print("Enter String Which You Want To Send:- "); | |
Scanner s=new Scanner(System.in); | |
String str=s.next(); | |
dos.writeUTF(str); | |
InputStream s1In = s1.getInputStream(); | |
DataInputStream dis = new DataInputStream(s1In); | |
String st = new String (dis.readUTF()); | |
System.out.println(st); | |
dis.close(); | |
s1In.close(); | |
s1.close(); | |
}//end while | |
} | |
} |
SocketServer.java work as Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
import java.io.*; | |
public class SocketServer | |
{ | |
public static void main(String args[]) throws IOException | |
{ | |
ServerSocket s = new ServerSocket(4444); | |
try | |
{ | |
while(true) | |
{ | |
Socket s1=s.accept(); | |
InputStream s1In = s1.getInputStream(); | |
DataInputStream dis = new DataInputStream(s1In); | |
String st = new String (dis.readUTF()); | |
System.out.println(st); | |
OutputStream s1out = s1.getOutputStream(); | |
DataOutputStream dos = new DataOutputStream (s1out); | |
System.out.print("Enter String Which You Want To Send:- "); | |
Scanner s=new Scanner(System.in); | |
String str=s.next(); | |
dos.writeUTF(s); | |
dos.close(); | |
s1out.close(); | |
s1.close(); | |
} | |
} | |
catch(Exception e) | |
{ | |
System.out.println(e); | |
} | |
} | |
} |
No comments:
Post a Comment