JFrame

 

实现了客服端,服务器的连接

客户端源码:s

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Client {
    static DataOutputStream out=null;

    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        Socket mysocket = null;
        DataInputStream in = null;
        //final DataOutputStream out = null;
        Thread readData;
        Read read = null;
        try {
            mysocket = new Socket();
            read = new Read();
            readData = new Thread(read);
            System.out.print("输入服务器的IP:");
            String IP = scanner.nextLine();
            System.out.print("输入端口号:");
            int port = scanner.nextInt();
            if (mysocket.isConnected()) {
            } else {
                InetAddress address = InetAddress.getByName(IP);
                InetSocketAddress socketAddress = new InetSocketAddress(address, port);
                mysocket.connect(socketAddress);
                in = new DataInputStream(mysocket.getInputStream());
                out = new DataOutputStream(mysocket.getOutputStream());
                read.setDataInputStream(in);
                readData.start();
            }
        } catch (Exception e) {
            System.out.println("服务器已断开" + e);
        }
        //System.out.print("输入园的半径(放弃请输入N):");
        JFrame jFrame = new JFrame("输入学号");

        JPanel jPanel = new JPanel();

        JLabel jLabel = new JLabel("学号:");
        jPanel.add(jLabel);

        JTextField jTextField = new JTextField(15);
        jPanel.add(jTextField);

        JButton jButton = new JButton("  发送  ");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("我呗点击了一下");
                String str = jTextField.getText();
                try {
                    out.writeUTF(str);
                    System.out.println(".actionPerformed()");
                } catch (IOException ex) {
                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        jPanel.add(jButton);
        jFrame.add(jPanel);

        jFrame.setSize(400, 400);
        jFrame.setVisible(true);
        //  jFrame.setResizable(false);//关闭放大窗口
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       

    }
}

 read端

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Read implements Runnable {
   DataInputStream in;
   public void setDataInputStream(DataInputStream in) {
      this.in = in;
   }
   public void run() {
      double result=0;
       try {
           String answer=in.readUTF();
           String kkk=new String(answer);
           System.out.println(kkk);
           System.out.println("接收到的answer字符串为:"+answer);
           System.out.println("下面对程序进行处理:");
           int sum = 0;
//           String value = "192.168.128.33";
           // 注意要加\\,要不出不来,yeah
           answer=answer.substring(12);
           System.out.println("截取完学号之后为:"+answer);
           String[] names = answer.split("\\,");
           for (int i = 0; i < names.length; i++) {
               sum += Integer.parseInt(names[i]);
               System.out.println(names[i]);
           }
           System.out.println("sum=" + sum);
           double c = 0.0;
           c = (double) sum / names.length;
           System.out.println("c=" + c);
           
           Connection conn=null;
           Statement sta=null;
          try {
              Class.forName("com.mysql.jdbc.Driver"); 
              String url = "jdbc:mysql://172.28.22.100:3306/2017jsj2?characterEncoding=utf-8";
                String dbusername = "2017jsj2";
                String dbpassword = "123abc";
                conn = DriverManager.getConnection(url, dbusername, dbpassword);
                sta=conn.createStatement();
                String a = " " + sum;
                String b=" "+answer;
                String sql="insert into Test35 (name,pwd) values('"+kkk+"','"+a+"') ";
                int row=sta.executeUpdate(sql);
                System.out.println(row);
                
          } catch (ClassNotFoundException ex) {
              Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
          } catch (SQLException ex) {
              Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
          }finally{
              if(sta!=null){
                  try {
                      sta.close();
                  } catch (SQLException ex) {
                      Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
              if(conn!=null){
                  try {
                      conn.close();
                  } catch (SQLException ex) {
                      Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
          }
               
       } catch (IOException ex) {
           Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
       }
       
      
//      while(true) {
//        try{ result=in.readDouble();
//             System.out.println("圆的面积:"+result);
//             System.out.print("输入园的半径(放弃请输入N):");
//        }
//        catch(IOException e) { 
//             System.out.println("与服务器已断开"+e);
//             break;
//        }   
//      }
   }
}

服务器端 


import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
   public static void main(String args[]) {
      ServerSocket server=null;
      ServerThread thread;
      Socket you=null;
      while(true) {
        try{  server=new ServerSocket(2010);
        }
        catch(IOException e1) { 
              System.out.println("正在监听"); //ServerSocket对象不能重复创建
        } 
        try{  System.out.println(" 等待客户呼叫");
              you=server.accept();
              System.out.println("客户的地址:"+you.getInetAddress());
        } 
        catch (IOException e) {
              System.out.println("正在等待客户");
        }
        if(you!=null) { 
              new ServerThread(you).start(); //为每个客户启动一个专门的线程  
        }
      }
   }
}
class ServerThread extends Thread {
   Socket socket;
   DataOutputStream out=null;
   DataInputStream  in=null;
   String s=null;
   ServerThread(Socket t) {
      socket=t;
      try {  out=new DataOutputStream(socket.getOutputStream());
             in=new DataInputStream(socket.getInputStream());
      }
      catch (IOException e){}
   }  
   public void run() {        
      while(true) {
         try{  
              //double r=in.readDouble();//堵塞状态,除非读取到信息
              // double area=Math.PI*r*r;
               //out.writeDouble(area);
               String stuid=in.readUTF();
               System.out.println("服务器端接收到的学生学号为:"+stuid);
               StringBuffer sb=new StringBuffer();
               sb.append(stuid);
               Random r = new Random();
               for( int i =0;i<3;i++){
                   int ran=r.nextInt(100);
                   sb.append(Integer.toString(ran)+",");
               }
               sb.deleteCharAt(sb.length()-1);
               String answer=new String(sb);
               System.out.println("返回给用户的学号+随机数为:");
               System.out.println(answer);
               out.writeUTF(answer);
         }
         catch (IOException e) {
               System.out.println("客户离开");
               return;
         }
      }
   } 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值