`

socket服务与客户对接

阅读更多
现在贴工程1:
package com.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class NioServer {
public static final int SERVERPORT=5555;
public static final String USERNAME="wangzhirong";
public static final String PASSWORD="123456";
public static final String ISACK="ACK";
public static final String ISNAK="NAK!";
// Selector selector;//选择器
// SelectionKey key;//key。 一个key代表一个Selector 在NIO通道上的注册,类似主键;
//      //取得这个Key后就可以对Selector在通道上进行操作
private ByteBuffer echoBuffer = ByteBuffer.allocate( 1024 );// 通道数据缓冲区
public NioServer(){
}
public static void main(String[] args) throws IOException {
  NioServer ns=new NioServer();
  ns.BuildNioServer();
}
public void BuildNioServer() throws IOException{
  /////////////////////////////////////////////////////////
  ///////先对服务端的ServerSocket进行注册,注册到Selector ////
  /////////////////////////////////////////////////////////
  ServerSocketChannel ssc = ServerSocketChannel.open();//新建NIO通道
  ssc.configureBlocking( false );//使通道为非阻塞
  ServerSocket ss = ssc.socket();//创建基于NIO通道的socket连接
  //新建socket通道的端口
  ss.bind(new InetSocketAddress("127.0.0.1",SERVERPORT));
  Selector selector=Selector.open();//获取一个选择器
  //将NIO通道选绑定到择器,当然绑定后分配的主键为skey
  SelectionKey skey = ssc.register( selector, SelectionKey.OP_ACCEPT );
  ////////////////////////////////////////////////////////////////////
  ////   接收客户端的连接Socket,并将此Socket也接连注册到Selector   ////
  ///////////////////////////////////////////////////////////////////
  while(true){
   int num = selector.select();//获取通道内是否有选择器的关心事件
   if(num<1){continue; }
   Set selectedKeys = selector.selectedKeys();//获取通道内关心事件的集合
   Iterator it = selectedKeys.iterator();
   while (it.hasNext()) {//遍历每个事件
    try{
     SelectionKey key = (SelectionKey)it.next();
     //有一个新联接接入事件,服务端事件
     if ((key.readyOps() & SelectionKey.OP_ACCEPT)
               == SelectionKey.OP_ACCEPT) {
              // 接收这个新连接
              ServerSocketChannel serverChanel = (ServerSocketChannel)key.channel();
              //从serverSocketChannel中创建出与客户端的连接socketChannel
              SocketChannel sc = serverChanel.accept();
              sc.configureBlocking( false );
              // Add the new connection to the selector
              // 把新连接注册到选择器
              SelectionKey newKey = sc.register( selector,
                SelectionKey.OP_READ );
              it.remove();
              System.out.println( "Got connection from "+sc );
     }else
     //读客户端数据的事件,此时有客户端发数据过来,客户端事件
     if((key.readyOps() & SelectionKey.OP_READ)
               == SelectionKey.OP_READ){
              // 读取数据
              SocketChannel sc = (SocketChannel)key.channel();
              int bytesEchoed = 0;
              while((bytesEchoed = sc.read(echoBuffer))> 0){
               System.out.println("bytesEchoed:"+bytesEchoed);
              }
              echoBuffer.flip();
              System.out.println("limet:"+echoBuffer.limit());
              byte [] content = new byte[echoBuffer.limit()];
              echoBuffer.get(content);
              String result=new String(content);
              doPost(result,sc);
              echoBuffer.clear();
              it.remove();
     }  
    }catch(Exception e){}
   }
  }
}

public void doPost(String str,SocketChannel sc){
  boolean isok=false;
  int index=str.indexOf('|');
  if(index>0){
   String name=str.substring(0,index);
   String pswd=str.substring(index+1);
   if(pswd==null){pswd="";}
   if(name!=null){
    if(name.equals(USERNAME)
     && pswd.equals(PASSWORD)
    ){
     isok=true;    
    }else{
     isok=false;
    }   
   }else{
    isok=false;
   }  
  }else{
   isok=false;
  }
  String result="";
  if(isok){
   result="ACK";
  }else{
   result="NAK!";
  }
  ByteBuffer bb = ByteBuffer.allocate( result.length() );
  bb.put(result.getBytes());
  bb.flip();
  try {
   sc.write(bb);
  } catch (IOException e) {
   e.printStackTrace();
  }
  bb.clear();
}

}

下面贴工程2
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class UserClient implements  ActionListener{
JFrame jf;
JPanel jp;
JLabel label_name;
JLabel label_pswd;
JTextField userName;
JButton jb;
JPasswordField paswrd;
JLabel hintStr;
public UserClient  (){
  jf=new JFrame("XXX 登陆系统");
  jp=new JPanel();
  jf.setContentPane(jp);
  jf.setPreferredSize(new Dimension(350,220));
  jp.setPreferredSize(new Dimension(350,220));
  jp.setBackground(Color.gray);
  label_name=new JLabel();
  label_name.setPreferredSize(new Dimension(150,30));
  label_name.setText("请输入帐户(数字或英文):");
  userName=new JTextField();
  userName.setPreferredSize(new Dimension(150,30));
  jp.add(label_name);
  jp.add(userName); 
  label_pswd=new JLabel();
  label_pswd.setPreferredSize(new Dimension(150,30));
  label_pswd.setText("请输入密码:");
  jp.add(label_pswd);
  paswrd=new JPasswordField();
  paswrd.setPreferredSize(new Dimension(150,30));
  jp.add(paswrd);
  jb=new JButton("OK");
  jb.setPreferredSize(new Dimension(150,30));
  jb.setText("确  定");
  jb.addActionListener( this);
  jp.add(jb);
  hintStr=new JLabel();
  hintStr.setPreferredSize(new Dimension(210,40));
  hintStr.setText("");
  hintStr.setForeground(Color.RED);
  jp.add(hintStr);
  jf.pack();
  jf.setVisible(true);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private String name;
private String pswd;
public void actionPerformed(ActionEvent e) {
  name=userName.getText().trim();
  pswd=new String(paswrd.getPassword());
  if(pswd==null){
   pswd="";
  }else{
   pswd=pswd.trim();
  }
  if(name!=null && name.length()>0){
   hintStr.setText("正在验证客户端,请稍候...");
   start();
  }
}

OutputStream os;
Socket s;
InputStream is;
public void start(){
  //建立联网线程
  new Thread(new Runnable(){
   public void run() {
    try {
     s=new Socket("127.0.0.1",5555);
     //写
     os=s.getOutputStream();
     os.write(name.getBytes());
     os.write('|');//用户名与密码用"|"分隔
     os.write(pswd.getBytes());
     os.flush();
     //读内容
     Thread.sleep(1000);
     is=s.getInputStream();
     int len=is.available();
     System.out.println("len:"+len);
     byte[] bytes=new byte[len];
     is.read(bytes);
     String resut=new String(bytes);
     System.out.println("resut:"+resut);
     //TODO 这里通过返回结果处理
     if(resut.equals("ACK")){
      hintStr.setText("验证成功,欢迎光临!");
     }else{
      paswrd.setText(null);
      hintStr.setText("用户名或密码错误,请重新输入");
     }
    } catch (UnknownHostException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }finally{
//     try {
//      os.close();
//      is.close();
//      s.close();
//     } catch (IOException e) {
//      e.printStackTrace();
//     }
    }
   }   
  }).start();
}

public static void main(String[] args) {
  new UserClient();
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics