标签:image 函数 过程 break etl 第四次 sys 127.0.0.1 [1]
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
客户端:
Client_Main.java
import java.net.*; import java.util.*; public class Client_Main{ public static void main (String args[]){ Scanner scanner=new Scanner(System.in); Thread readData; ReceiveLetterForZhang receiver=new ReceiveLetterForZhang(); try{ readData=new Thread(receiver); readData.start(); byte [] buffer=new byte[1]; InetAddress address=InetAddress.getByName("127.0.0.1"); DatagramPacket dataPack=new DatagramPacket(buffer,buffer.length,address,666); 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); } } }
ReceiveLetterForZhang.java
import java.net.*; public class ReceiveLetterForZhang 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(888); } 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){} } } }
服务器端:
Server_Main.java
import java.net.*;
import java.util.*;
public class Server_Main{
public static void main (String args[]){
Scanner scanner=new Scanner(System.in);
Thread readData;
ReceiveLetterForLi receiver=new ReceiveLetterForLi();
try{
readData=new Thread(receiver);
readData.start();
byte [] buffer=new byte[1];
InetAddress address=InetAddress.getByName("127.0.0.1");
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);
}
}
}
ReceiveLetterForLi.java
import java.net.*; public class ReceiveLetterForLi 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){} } } }
二:将所需数据提出,建立SQL表格
创建了数据库和sql表格,但是无法实现聊天内容数据的插入
三:实现数据库
GetDBConnection.java
import java.sql.*; public class GetDBConnection { public static Connection connectDB(String DBName,String id,String p){ Connection con=null; String uri="jdbc:mysql://localhost:3306/"+DBName+"?useSSL=true&characterEncoding=utf-8"; try{ Class.forName("com.mysql.jdbc.Driver"); } catch(Exception e){} try{ con=DriverManager.getConnection(uri,id,p); } catch(SQLException e){} return con; } }
运行结果:
过程性考核总结:
采用书上张三李四为例子进行编程,由于这部分的知识学习的很薄弱,具体运行程序没能编程成功,我会在休息时间好好复习
标签:image 函数 过程 break etl 第四次 sys 127.0.0.1 [1]
原文地址:https://www.cnblogs.com/mengxiang1998/p/10111467.html