标签:sql runnable img value can receive pack 服务器 文件中
码云地址:https://gitee.com/c_cy/four
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
//服务器端
import java.net.*; import java.util.*; import java.sql.*; public class Server_Main { public static void main(String[] args) { /*Connection con=null; Statement sql; ResultSet rs; con =GetDBConnection.connectDB("students","root","111111"); if (con==null)return;*/ Scanner scanner=new Scanner(System.in); Thread readData; // ReceiveLetterForClient receiver=new ReceiveLetterForClient(); 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(); /*String jilu="(mess,null)"; String sqlStr="insert into mess values"+jilu;*/ 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); } /*try{ sql=con.createStatement(); int ok=sql.executeUpdate(sqlStr); rs=sql.executeQuery("select * from client_学号"); while(rs.next()){ String mess=rs.getString(1); String time=rs.getString(2); System.out.print(mess); System.out.print(time); } con.close(); } catch(SQLException e){ System.out.println(e); }*/ } }
import java.net.*; public class ReceiveLetterForServer 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.printf("%25s\n","收到:"+message); } catch(Exception e){} } } } }
//客户端
import java.net.*; import java.util.*; public class Client_Main { public static void main(String[] args) { /*Connection con=null; Statement sql; ResultSet rs; con =GetDBConnection.connectDB("students","root","111111"); if (con==null)return;*/ Scanner scanner=new Scanner(System.in); Thread readData; ReceiveLetterForServer receiver=new ReceiveLetterForServer(); 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(); /*String jilu="(mess,null)"; String sqlStr="insert into mess values"+jilu;*/ 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); } /*try{ sql=con.createStatement(); int ok=sql.executeUpdate(sqlStr); rs=sql.executeQuery("select * from client_学号"); while(rs.next()){ String mess=rs.getString(1); String time=rs.getString(2); System.out.print(mess); System.out.print(time); } con.close(); } catch(SQLException e){ System.out.println(e); }*/ } }
import java.net.*; public class ReceiveLetterForClient 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.printf("%25s\n","收到:"+message); } catch(Exception e){} } } } }
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(""); } catch(Exception e){} try{ con=DriverManager.getConnection(uri,id,p); } catch(SQLException e){} return con; } }
建SQL表
总结:
实现了服务器与客户端之间的通信,代码中大块的注释是用来实现将通信的内容存入数据库的,但没有成功所以注释掉了
这次的考核主要是线程和数据库,我觉得分开时编程没有太大的困难,但当这两个合在一起时,出现了问题,我没有解决,
可能是因为学的不够扎实,理解的还不透彻!
标签:sql runnable img value can receive pack 服务器 文件中
原文地址:https://www.cnblogs.com/ccyan/p/10114088.html