2016/04/26(Java网络编程)

package com.wode.test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.DeflaterOutputStream;

/**
 *
 * @author Administrator
 *建立一个 服务器
 */
public class Myserver extends Thread{//继承接口  Thread

 private ServerSocket serversoket;
 public  Myserver(int port) throws IOException{//需要参数  也就是端口
   serversoket = new ServerSocket(port);//创建一个 服务器
  //serverSocket.setSoTimeout(10000);//setSoTimeout 多少毫秒没人连接就关闭服务器(服务器超时)
  
 }
 @Override
 public void run() {//重写线程的run  方法 让线程执行
  while (true) {//让线程循环
   try {
    //System.out.println(serversoket.getLocalPort()+"在这个窗口等待连接");
    //getLocalSocketAddress() 返回此套接字绑定的端点的地址,如果尚未绑定则返回 null
    Socket socket = serversoket.accept();//accept()  侦听并接受到此套接字的连接
    //打印谁连接了我们的服务器
    //System.out.println(socket.getRemoteSocketAddress()+"连接了服务器");
    
    //服务器发送消息给 客户端
    DataOutputStream dStream = new DataOutputStream(socket.getOutputStream());
    //getOutputStream() 返回此套接字的输出流
    //DataOutputStream  数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中
    dStream.writeUTF("我是网吧小妹 你要干嘛?");
    //接收 客户端信息
    DataInputStream doStream = new DataInputStream(socket.getInputStream());
    String string = doStream.readUTF();
    //System.out.println(string);
    //判断信息 并回复客户端
    switch (string) {
    case "你好":
     dStream.writeUTF("网吧小妹也对你说你好");
     break;
    case "我爱你":
     dStream.writeUTF("我也爱你");
     break;
    case "充值":
     dStream.writeUTF("充值成功");
     break;
    case "买烟":
     dStream.writeUTF("给你烟");
     break;
    case "约吗":
     dStream.writeUTF("叔叔不约");
     break;
    case "买饮料":
     dStream.writeUTF("给你饮料");
     break;
    case "伤心":
     dStream.writeUTF("心若向阳 无惧悲伤");
     break;
    case "滚蛋":
     dStream.writeUTF("别对我那么凶嘛");
     break;
    case "失恋":
     dStream.writeUTF("天涯何处无芳草  何必单恋一支花");
     break;

    default:
     dStream.writeUTF("我不知道你在说什么诶= =");
     break;
    }
    
    
    //询问 顾客需要什么
    //dStream.writeUTF("请选择:1-充值 2-买烟 3-买饮料");
    //接收顾客选择的信息
    //DataInputStream doStream1 = new DataInputStream(socket.getInputStream());
    //String string1 = doStream.readUTF();
    //System.out.println(string1);
    //并且进行判断
//    if (string1.equals("1")) {
//     dStream.writeUTF("充值成功!");
//     return;
//    }if (string1.equals("2")) {
//     dStream.writeUTF("给你烟");
//     return;
//    } if(string1.equals("3")) {
//     dStream.writeUTF("给你饮料");
//     return;
//    }else {
//     dStream.writeUTF("干啥啊 想撩妹? 滚蛋");
//    }
    
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 public static void main(String[] args) {
  try {
   Thread thread = new Myserver(9876);
   thread.start();//start() 让线程开始
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
 

 

 

package com.wode.test;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Administrator 创建 用户
 */
public class MrchenUser extends JFrame {
   JLabel jlLabel;
   JTextField txtinput;
 public MrchenUser() {
  this.setSize(400, 300);// 设置窗体大小
  this.setLocationRelativeTo(null);// 居中显示
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 可关闭性
  this.setResizable(false);// 禁止用户放大窗口

  JPanel jPanel = new JPanel();
  jPanel.setLayout(null); // 设置自由布局
  jPanel.setBounds(1, 1, 500, 400);// 容器大小和窗口一样大

  // 创建一个显示窗口
     jlLabel = new JLabel("我是网吧小妹 你要干嘛?");
  jlLabel.setFont(new Font("正楷", Font.BOLD, 20));
  jlLabel.setBounds(0, 0, 400, 80);
  jPanel.add(jlLabel);

  // 创建一个输入框
     txtinput = new JTextField();
  txtinput.setBounds(0, 135, 400, 100);
  jPanel.add(txtinput);

  // 绑定输入框事件
  txtinput.addFocusListener(new txtinput(txtinput,jlLabel));

  // 创建一个 发送按钮
  JButton okbtn = new JButton("发送");
  okbtn.setBounds(245, 240, 60, 30);
  jPanel.add(okbtn);

  // 绑定事件
  okbtn.addActionListener(new okListener(txtinput, jlLabel));

  // 创建一个 取消 按钮
  JButton cancelbtn = new JButton("取消");
  cancelbtn.setBounds(320, 240, 60, 30);
  jPanel.add(cancelbtn);

  this.add(jPanel);
  this.setVisible(true);
 }

 public static void main(String[] args) {
  new MrchenUser();
//  try {
//   Socket socket = new Socket("localhost", 9876);
//   //接收并且 显示在Swing 上面
//   DataInputStream dStream = new DataInputStream(socket.getInputStream());
//   String string = dStream.readUTF();
//   //System.out.println(string);
//   jlLabel.setText(string);
//   //把输入框的东西传给 服务器
//   
//  } catch (UnknownHostException e) {
//   // TODO Auto-generated catch block
//   e.printStackTrace();
//  } catch (IOException e) {
//   // TODO Auto-generated catch block
//   e.printStackTrace();
//  }
 
 }
}

/**
 *
 * @author Administrator 发送按钮 事件
 */
class okListener implements ActionListener {
 private JTextField txtinput;
 private JLabel jlLabel;
 public okListener(JTextField txtinput,JLabel jlLabel){
  this.txtinput = txtinput;
  this.jlLabel = jlLabel;
 }

 @Override
 public void actionPerformed(ActionEvent arg0) {
  // TODO Auto-generated method stub
  //System.out.println("已经发送");
  //System.out.println(txtinput.getText());
  try {
   Socket socket = new Socket("localhost", 9876);
   //接收并且 显示在Swing 上面
   DataInputStream dStream = new DataInputStream(socket.getInputStream());
   String string = dStream.readUTF();
   //System.out.println(string);
   jlLabel.setText(string);
   //把输入框的东西传给 服务器
   DataOutputStream doStream = new DataOutputStream(socket.getOutputStream());
   doStream.writeUTF(txtinput.getText());
   //接收
   DataInputStream dStream1 = new DataInputStream(socket.getInputStream());
   String string1 = dStream.readUTF();
   jlLabel.setText(string1);
  } catch (UnknownHostException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

}

class txtinput implements FocusListener {
 private JTextField txtinput;
 private JLabel jlLabel;

 public txtinput(JTextField txtinput,JLabel jlLabel) {
  this.txtinput = txtinput;
  this.jlLabel = jlLabel;
 }

 @Override
 public void focusGained(FocusEvent arg0) {
  // TODO Auto-generated method stub
  //System.out.println("获得焦点");
//  if (txtinput.getText().length()!= 0) {
//   jlLabel.setText("我是网吧小妹 你要干嘛?");
//
//  }
 }

 @Override
 public void focusLost(FocusEvent arg0) {
  // TODO Auto-generated method stub
  //System.out.println("失去焦点");
  if (txtinput.getText().length() == 0) {
   jlLabel.setText("我是网吧小妹 你要干嘛?");

  }
 }

}

posted @ 2016-04-26 21:19  丨她心如海般难测  阅读(103)  评论(0编辑  收藏  举报