Last active
May 8, 2016 22:06
-
-
Save agaikwad123/22bd6046bf11d91390ea0977b7269137 to your computer and use it in GitHub Desktop.
Socket Programming
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
//Server// | |
import java.io.*; | |
import java.net.*; | |
public class server { | |
public static void main(String args[]) throws Exception { | |
String clientSentence; | |
String capitalizedSentence; | |
ServerSocket welcomeSocket = new ServerSocket(6789); | |
//while(true) | |
{ | |
Socket connectionSocket = welcomeSocket.accept(); | |
BufferedReader inFromClient = new BufferedReader(new | |
InputStreamReader(connectionSocket.getInputStream())); | |
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); | |
clientSentence=inFromClient.readLine(); | |
System.out.println("Message from client : "+ clientSentence); | |
} | |
} | |
} |
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
//CLIENT11// | |
java.io.*; | |
import java.net.*; | |
class client11 | |
{ | |
public static void main(String args[]) | |
{ | |
String data; | |
try | |
{ | |
DatagramSocket s=new DatagramSocket(); | |
do | |
{ | |
System.out.println("Enter the data"); | |
DataInputStream dis=new DataInputStream(System.in); | |
InetAddress ip=InetAddress.getByName(""); | |
byte[] senddata=new byte[1024]; | |
byte[] receivedata=new byte[1024]; | |
data=dis.readLine(); | |
senddata=data.getBytes(); | |
DatagramPacket sp=new DatagramPacket(senddata,senddata.length,ip,8050); | |
s.send(sp); | |
DatagramPacket rp=new DatagramPacket(receivedata,receivedata.length); | |
s.receive(rp); | |
String msg=new String(rp.getData()); | |
System.out.println("From Server:"+msg); | |
} | |
while(!data.equalsIgnoreCase("end")); | |
s.close(); | |
} | |
catch(Exception e){} | |
}} |
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
//server11// | |
import java.io.*; | |
import java.net.*; | |
class server11 | |
{ | |
public static void main(String args[]) | |
{ | |
try | |
{ | |
DatagramSocket s=new DatagramSocket(8050); | |
byte[] rd=new byte[1024]; | |
byte[] sd=new byte[1024]; | |
String sent; | |
do | |
{ | |
DatagramPacket rp=new DatagramPacket(rd,rd.length); | |
s.receive(rp); | |
String data=new String(rp.getData()); | |
System.out.println("Received from client:"+data); | |
InetAddress ip=rp.getAddress(); | |
System.out.println("Address:"+ip); | |
int port=rp.getPort(); | |
System.out.println("Port:"+port); | |
System.out.println("Enter the data to send"); | |
DataInputStream dis=new DataInputStream(System.in); | |
sent=dis.readLine(); | |
sd=sent.getBytes(); | |
DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,port); | |
s.send(sp); | |
} | |
while(!sent.equalsIgnoreCase("end")); | |
s.close(); | |
} | |
catch(IOException e){} | |
}} |
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
//CLIENT// | |
import java.io.*; | |
import java.net.*; | |
public class client{ | |
public static void main(String argv[]) throws Exception { | |
String sentence; | |
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); | |
Socket clientSocket = new Socket("localhost", 6789); | |
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); | |
BufferedReader inFromServer = new BufferedReader(new | |
InputStreamReader(clientSocket.getInputStream())); | |
sentence = inFromUser.readLine(); | |
outToServer.writeBytes(sentence); | |
clientSocket.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment