java实现tcp,udp消息传输
tcp
public class Tcplearn1 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9999);
OutputStream stream = socket.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(new File("1.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len=fileInputStream.read(bytes))!=-1)
stream.write(bytes,0,len);
fileInputStream.close();
stream.close();
socket.close();
}
}
public class Tcprecive {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(new File("2.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fileOutputStream.write(buffer,0,len);
}
fileOutputStream.close();
is.close();
socket.close();
serverSocket.close();
}
}
udp
public class udpclient {
public static void main(String[] args) throws IOException {
//建立一个socket
DatagramSocket datagramSocket = new DatagramSocket();
//建消息包
String msg="hello server";
InetAddress localhost = InetAddress.getByName("localhost");
int port=9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//send
datagramSocket.send(packet);
datagramSocket.close();
}
}
public class udpserver {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(9090);
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(packet);//阻塞接收
System.out.println(packet.getSocketAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
datagramSocket.close();
}
}
输入消息传输
public class udpclient {
public static void main(String[] args) throws IOException {
//建立一个socket
DatagramSocket datagramSocket = new DatagramSocket();
//建消息包
//控制台读取输入
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String readLine = reader.readLine();
byte[] bytes = readLine.getBytes();
InetAddress localhost = InetAddress.getByName("localhost");
int port=6666;
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, localhost, port);
//send
datagramSocket.send(packet);
datagramSocket.close();
}
}
public class udpserver {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket = new DatagramSocket(6666);
while (true){
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(packet);//阻塞接收
byte[] data = packet.getData();
String s = new String(data, 0, packet.getLength());
System.out.println(s);
if (s.equals("bye"))break;
}
datagramSocket.close();
}
}
udp对话
public class TalkReceive implements Runnable {
DatagramSocket datagramSocket =null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom=msgFrom;
try {
datagramSocket= new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
try {
datagramSocket.receive(packet);//阻塞接收
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = packet.getData();
String s = new String(data, 0, packet.getLength());
System.out.println(msgFrom+": "+s);
if (s.equals("bye"))break;
}
datagramSocket.close();
}
}
public class Talksend implements Runnable {
private DatagramSocket datagramSocket=null;
private BufferedReader reader=null;
private int fromPort;
private int toPort;
private String toIp;
private String msgSender;
public Talksend(int fromPort, String toIp, int toPort,String msgSender) throws SocketException {
this.fromPort=fromPort;
this.toIp = toIp;
this.toPort = toPort;
this.msgSender=msgSender;
datagramSocket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
}
@Override
public void run() {
try {
while (true){
String readLine = reader.readLine();
byte[] bytes = readLine.getBytes();
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length,new InetSocketAddress(this.toIp,this.toPort));
//send
datagramSocket.send(packet);
if (readLine.equals("bye")) break;
}
datagramSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class TalkTeacher {
public static void main(String[] args) throws SocketException {
new Thread(new Talksend(5555,"localhost",8888,"teacher")).start();
new Thread(new TalkReceive(9999,"teacher")).start();
}
}
public class TalkStudent {
public static void main(String[] args) throws SocketException {
new Thread(new Talksend(7777,"localhost",9999,"student")).start();
new Thread(new TalkReceive(8888,"student")).start();
}
}