1、通信软件的功能
有客户端和服务器端,实现客户端和服务器相互不停的发送和接收消息。 要求发送和接收消息可以同时进行。
使用tcp实现
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class ReadData extends Thread {
private Socket socket;
public ReadData(Socket socket){
this.socket=socket;
}
@Override
public void run() {
super.run();
InputStream inputStream=null;
try {
inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int read = inputStream.read(bytes);
while (read!=-1){
System.out.println(new String(bytes,0,read));
read=inputStream.read(bytes);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class WriteData extends Thread {
private Socket socket;
public WriteData(Socket socket){
this.socket=socket;
}
@Override
public void run() {
super.run();
OutputStream outputStream =null;
while (true){
Scanner scanner = new Scanner(System.in);
String s=scanner.nextLine();
try {
outputStream = socket.getOutputStream();
outputStream.write(s.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.net.Socket;
public class My {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 9000);
new WriteData(socket).start();
new ReadData(socket).start();
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class You {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9000);
Socket accept = serverSocket.accept();
new ReadData(accept).start();
new WriteData(accept).start();
}
使用Udp实现
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class My implements Runnable {
private int dkh;
public My(int dkh){
this.dkh=dkh;
}
@Override
public void run() {
while (true) {
udpMy();
}
}
void udpMy() {
DatagramSocket datagramSocket = null;
DatagramPacket datagramPacket = null;
try {
datagramSocket = new DatagramSocket();
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
byte[] bytes = s.getBytes();
InetAddress locahost = InetAddress.getByName("localhost");
datagramPacket = new DatagramPacket(bytes, 0, bytes.length, locahost, dkh);
datagramSocket.send(datagramPacket);
} catch (Exception e) {
} finally {
}
}
}
package ParcticeDiwuzhou.lx0813.lx.udptx;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class You implements Runnable {
private int dkh;
public You(int dkh){
this.dkh=dkh;
}
@Override
public void run() {
while (true) {
udpYou();
}
}
void udpYou() {
DatagramSocket datagramSocket = null;
DatagramPacket datagramPacket = null;
try {
datagramSocket = new DatagramSocket(dkh);
byte[] bytes = new byte[1024];
datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(datagramPacket);
System.out.print("You:");
System.out.println(new String(bytes));
} catch (Exception e) {
} finally {
}
}
}
public class MyMianThread {
public static void main(String[] args) {
Lock lock=new ReentrantLock();
My my = new My(9600);
Thread thread = new Thread(my);
thread.start();
You you = new You(9601);
Thread thread1 = new Thread(you);
thread1.start();
}
}
public class YouMianThread {
public static void main(String[] args) {
Lock lock=new ReentrantLock();
You you = new You(9600);
Thread thread1 = new Thread(you);
thread1.start();
My my = new My(9601);
Thread thread = new Thread(my);
thread.start();
}
}
|