import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;public class test_udp_send { public static void main(String arg[]) throws Exception{ DatagramSocket dSocket=new DatagramSocket(3000); String string ="Hello,word"; DatagramPacket datagramPacket=new DatagramPacket(string.getBytes(), string.length(),InetAddress.getByName("127.0.0.1"),8001); dSocket.send(datagramPacket); dSocket.close(); }}
import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;public class test_udp_server { public static void main(String args[]) throws Exception{ byte buf[]=new byte[1024]; DatagramSocket dSocket= new DatagramSocket(8001); DatagramPacket datagramPacket=new DatagramPacket(buf, 1024); dSocket.receive(datagramPacket); String str=new String(datagramPacket.getData()); System.out.println(str); dSocket.close(); }}
import java.io.IOException;import java.io.InputStream;import java.net.InetAddress;import java.net.Socket;public class test_tcp_send {public static void main(String arg[]) throws Exception{ new client().connect(); }}class client{ public void connect() throws Exception, IOException{ Socket client=new Socket(InetAddress.getLocalHost(),8009); InputStream inputStream=client.getInputStream(); byte buf[]=new byte[1024]; int length=inputStream.read(buf); System.out.println(new String(buf)); client.close(); }}
public class test_tcp_server { public static void main(String args[]) throws Exception { new server().connect(); }}class server{ public void connect() throws Exception{ ServerSocket serverSocket=new ServerSocket(8009); Socket client=serverSocket.accept(); OutputStream outputStream=client.getOutputStream(); outputStream.write("hello,word".getBytes()); outputStream.close(); client.close(); }}