服务器和客户端都是自己再网上 copy 的代码,有一个 bug 就是当客户端退出的时候会报错,
这个 bug 很容易解决。先来看看我们的服务器端代码:(服务器端用了数据库的查询登陆)
package Test;
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.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Set;
public class Server {
public static final int serverport=5555;
public static final String isack="ack";
public static final String isnak="nak";
private
echobuffer=ByteBuffer.allocate(1024);
public Server() {
ByteBuffer
}
public static void main(String[] args) {
new Server().buildserver();
}
public void buildserver(){
try {
ServerSocketChannel
ssc=ServerSocketChannel.open();
ssc.configureBlocking(false);
ServerSocket ss=ssc.socket();
ss.bind(new
InetSocketAddress("127.0.0.1",serverport));
Selector selector=Selector.open();
SelectionKey
skey=ssc.register(selector,SelectionKey.OP_ACCEPT);
while(true)
{
int num=selector.select();
if(num<1)
{
continue;
}
Set selectedKeys=selector.selectedKeys();
Iterator it=selectedKeys.iterator();
while(it.hasNext())
{
SelectionKey key=(SelectionKey) it.next();
if((key.readyOps()&SelectionKey.OP_ACCEPT)==Sele
ctionKey.OP_ACCEPT)
{
ServerSocketChannel
serverchannel=(ServerSocketChannel) key.channel();
SocketChannel
sc=serverchannel.accept();
sc.configureBlocking(false);
SelectionKey
newKey=sc.register(selector,SelectionKey.OP_READ);
it.remove();
System.out.print("get connection
from"+sc);
}
else{
if((key.readyOps()&SelectionKey.OP_READ)==Selecti
onKey.OP_READ)
{
key.channel();
SocketChannel
sc=(SocketChannel)
int bytesEchoed=0;
while((bytesEchoed=sc.read(echobuffer))>0)
{
System.out.println("bytesEchoed"+bytesEchoed);
}
echobuffer.flip();
System.out.println("limet
"+echobuffer.limit());
byte
byte[echobuffer.limit()];
[]
content
=new
echobuffer.get(content);
String result=new String(content);
doPost(result,sc);
echobuffer.clear();
it.remove();
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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);
String sql="select name,password from login
where name='"+name+"'and password='"+pswd+"'";
SqlConn sc1=new SqlConn();
ResultSet rs=sc1.getResult(sql);
boolean f=false;
try {
f=rs.first();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(pswd==null)
{
pswd="";
}if(name!=null)
{
if(f)
{
isok=true;
}
else{
isok=false;
}
String result="";
if(isok)
{
result="ack";
}
else{
result="nak";
}
bb=ByteBuffer.allocate(result.length());
ByteBuffer
bb.put(result.getBytes());
bb.flip();
try {
sc.write(bb);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bb.clear();
}
}
}
}
下面是客户端:
package Test;
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.JTextField;
public class client extends JFrame{
private JLabel jLabel1;
private JTextField usertext;
private JLabel jLabel2;