标签:== 举例 使用 tap ORC 服务端 读取 height bytes
码云地址:https://gitee.com/shikexin/16012018_shi_kexin_examination/tree/master
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
客户端:
import java.net.*;
import java.util.*;
public class Client_Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
Thread readData;
ReceiveLetterForC receive=new ReceiveLetterForC();
try{
readData=new Thread(receive);
readData.start();
byte [] buffer=new byte [1];
InetAddress address=InetAddress.getByName("10.43.11.91");
DatagramPacket dataPack=
new DatagramPacket(buffer,buffer.length,address,888);
DatagramSocket postman=new DatagramSocket();
System.out.print("输入法送给服务器的消息:");
while(scanner.hasNext()){
String mess=scanner.nextLine();
buffer=mess.getBytes();
if(mess.length()==0)
System.exit(0);
buffer=mess.getBytes();
dataPack.setData(buffer);
postman.send(dataPack);
System.out.print("继续输入法送给服务器的消息:");
}
}
catch (Exception e){
System.out.println(e);
}
}
}
import java.net.*;
public class ReceiveLetterForC implements Runnable{
public void run(){
DatagramPacket pack=null;
DatagramSocket postman=null;
byte data[]=new byte[8192];
try{
pack=new DatagramPacket(data,data.length);
postman=new DatagramSocket(666);
}
catch(Exception e){}
while(true){
if(postman==null) break;
else
try{
postman.receive(pack);
String message=new String (pack.getData(),0,pack.getLength());
System.out.println("?"+message);
}
catch(Exception e){}
}
}
}
服务器:
import java.net.*;
import java.util.*;
//import java.io.*;
public class Server_Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
Thread readData;
ReceiveLetterForS receive=new ReceiveLetterForS();
try{
readData=new Thread(receive);
readData.start();
byte [] buffer=new byte [1];
InetAddress address=InetAddress.getByName("10.43.11.91");
DatagramPacket dataPack=
new DatagramPacket(buffer,buffer.length,address,888);
DatagramSocket postman=new DatagramSocket();
System.out.print("输入法送给客户端的消息:");
while(scanner.hasNext()){
String mess=scanner.nextLine();
buffer=mess.getBytes();
if(mess.length()==0)
System.exit(0);
buffer=mess.getBytes();
dataPack.setData(buffer);
postman.send(dataPack);
System.out.print("继续输入法送给客户端的消息:");
}
}
catch (Exception e){
System.out.println(e);
}
}
}
import java.net.*;
public class ReceiveLetterForS implements Runnable{
public void run(){
DatagramPacket pack=null;
DatagramSocket postman=null;
byte data[]=new byte[8192];
try{
pack=new DatagramPacket(data,data.length);
postman=new DatagramSocket(666);
}
catch(Exception e){}
while(true){
if(postman==null) break;
else
try{
postman.receive(pack);
String message=new String (pack.getData(),0,pack.getLength());
System.out.println("?"+message);
}
catch(Exception e){}
}
}
}
解题思路:使用套接字,UDP通信,使用DatagramSocket()方法构建对象
运行结果:

总结:问题是收不到消息,考核快结束,发现418页可以作为参考应用举例,这道题有点难,光靠课上学的知识真的不足以做出这道题,学到的知识也不是很扎实,课后也没有做练习,导致考核没有做出来,书翻得还不够多,java需要学习的地方还有很多,学无止境啊
| 学习内容 | 代码行数 | 博客字数 |
| 第四次过程性考核 | 120 | 150 |
标签:== 举例 使用 tap ORC 服务端 读取 height bytes
原文地址:https://www.cnblogs.com/shikexin/p/10122208.html