Tag:socket

Tag (socket)'s result:

java socket udp

Udpserver.java: import java.net.DatagramPacket; import java.net.DatagramSocket; public class Udpserver { public static void main(String[] args) throws Exception { System.out.println(“udp server is running……”); String str_send = “hi udpclient”; DatagramSocket se = new DatagramSocket(2016); byte[] by = new byte[1024]; DatagramPacket dp = new DatagramPacket(by,1024); while (true) { se.receive(dp); System.out.println(“the message from client is: \n”); String str_receive = new String(dp.getData(),0,dp.getLength()); System.out.println(str_receive); DatagramPacket se_send = new DatagramPacket(str_send.getBytes(),str_send.length(),dp.getAddress(),dp.getPort()); se.send(se_send); dp.setLength(1024); } //se.close(); } } Udpclient.java: import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class Udpclient { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(); String str = “hi server”; DatagramPacket dp = new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName(“localhost”),2016); ds.send(dp); byte[] by = new byte[1024]; DatagramPacket dp2 = new DatagramPacket(by,1024); ds.receive(dp2); System.out.println(new String(by,0,dp2.getLength())); ds.close(); } }

java netwrok program—tcp

I use socket in this program. TCPServer.java: import java.io.*; import java.net.*; import java.lang.*; import java.util.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(2345); while(true) { System.out.println(” Server is waitting………\n”); Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); Date transfer_date = new Date(); System.out.println(“Received: ” + clientSentence + “—” + transfer_date.toString()); //receive data from client capitalizedSentence = clientSentence.toUpperCase() + ‘\n’; outToClient.writeBytes(capitalizedSentence); //send data to client } } } TCPClient.java: import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); //get input from keyboard Socket clientSocket = new Socket(“localhost”, 2345); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + ‘\n’); //send data to server modifiedSentence = inFromServer.readLine(); System.out.println(“FROM SERVER: ” + modifiedSentence); //get data from server clientSocket.close(); } }