我一个网络程序,主要是成绩查询,一个是精确查询一个是模糊查询。
模糊查询的 连接的时候有点问题。郁闷啊。
望哪位高手能帮小弟 解决一下。
服务器端
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class MyServer
{
static final int PORT=1119;
static final int MAX=5;
public static void main(String[] args) throws IOException,SocketException
{
ServerService Server=new ServerService(PORT,MAX);
}
}
class ServerService extends Frame implements Runnable
{
ServerSocket jianting;
TextArea state;
Thread thread;
Socket lianjie;
boolean temp1,temp2;
String a="0";
String[] xinxi=new String[3];
String[] xinxi1=new String[3];
String[][] ziliao={
{"25","shuai","java","98"},
{"26","billy","c","66"},
{"27","xiaochong","java","88"},
{"28","hacker","c","75"}
};
boolean[] linshi=new boolean[4];
public ServerService(int port,int max)
{
try
{
setTitle("·þÎñÆ÷¿ªÊ¼¼àÌý");
jianting=new ServerSocket(port,max);
setLayout(new BorderLayout());
state=new TextArea("Server starting...\n",20,50);
this.addWindowListener(new WinAdpt());
add("Center",state);
setResizable(false); //ÉèÖô°¿Ú´óС²»¿É±ä
pack(); //°´Ê×Ñ¡³ß´ç·ÅÖøô°¿ÚµÄ×Ó×é¼þ¡£
show();
while(true)
{
lianjie=jianting.accept();
InetAddress ClientAddress=lianjie.getInetAddress();
state.appendText("¿Í»§¶ËÁ¬½ÓÀ´×Ô:"+ClientAddress.toString()+"\n");
thread=new Thread(this);
thread.start();
//state.append("µ±Ç°¿Í»§¶Ë¹Ø±Õ!"+"\n");
}
}catch(IOException e){}
}
public void run()
{
try
{
DataInputStream shuruliu=new DataInputStream(new BufferedInputStream(lianjie.getInputStream()));
PrintStream shuchuliu=new PrintStream(new BufferedOutputStream(lianjie.getOutputStream()));
while(true)
{
String jie=shuruliu.readLine();
if(jie.equals("###"))
jqchaxun();
else if(jie.equals("***"))
mhchaxun();
else if(jie.equals("##"))
break;
}
state.append("µ±Ç°¿Í»§¶Ë¹Ø±Õ!"+"\n");
thread.destroy();
}
catch(Exception e){}
}
public void jqchaxun()
{
try
{
DataInputStream shuruliu1=new DataInputStream(new BufferedInputStream(lianjie.getInputStream()));
PrintStream shuchuliu1=new PrintStream(new BufferedOutputStream(lianjie.getOutputStream()));
for(int i=0;i<3;i++)
{
String jie=shuruliu1.readLine();
xinxi[i]=jie;
}
boolean temp=true;
for(int k=0,j=0;k<4;k++)
if(xinxi[0].equals(ziliao[k][j])&&xinxi[1].equals(ziliao[k][j+1])&&xinxi[2].equals(ziliao[k][j+2]))
{
temp=false;
shuchuliu1.println("Sno:"+ziliao[k][j]);
shuchuliu1.flush();
shuchuliu1.println("Name:"+ziliao[k][j+1]);
shuchuliu1.flush();
shuchuliu1.println("Cource:"+ziliao[k][j+2]);
shuchuliu1.flush();
shuchuliu1.println("Performance:"+ziliao[k][j+3]);
shuchuliu1.flush();
break;
}
if(temp)
{
shuchuliu1.println("#");
shuchuliu1.flush();
}
}
catch(Exception e){}
}
public void mhchaxun()
{
try
{
DataInputStream shuruliu2=new DataInputStream(new BufferedInputStream(lianjie.getInputStream()));
PrintStream shuchuliu2=new PrintStream(new BufferedOutputStream(lianjie.getOutputStream()));
for(int i=0;i<3;i++)
{
String jie=shuruliu2.readLine();
xinxi1[i]=jie;
}
for(int n=0;n<4;n++)
linshi[n]=false;
for(int m=0;m<4;m++)
//for(int i=0;i<1;i++)
for(int j=0;j<xinxi1[0].length();j++)
{
char linshi1=xinxi1[0].charAt(j);
if(ziliao[m][0].indexOf((int)linshi1)!=-1&&!linshi[m])
{
linshi[m]=true;
break;
}
}
for(int m=0;m<4;m++)
// for(int i=0;i<1;i++)
for(int j=0;j<xinxi1[1].length();j++)
{
char linshi1=xinxi1[1].charAt(j);
if(ziliao[m][1].indexOf((int)linshi1)!=-1&&!linshi[m])
{
linshi[m]=true;
break;
}
}
for(int m=0;m<4;m++)
// for(int i=0;i<1;i++)
for(int j=0;j<xinxi1[2].length();j++)
{
char linshi1=xinxi1[2].charAt(j);
if(ziliao[m][2].indexOf((int)linshi1)!=-1&&!linshi[m])
{
linshi[m]=true;
break;
}
}
for(int i=0,j=0,k=0;i<4;i++,k++)
{
if(linshi[i])
{
shuchuliu2.println("Sno:"+ziliao[k][j]);
shuchuliu2.flush();
shuchuliu2.println("Name:"+ziliao[k][j+1]);
shuchuliu2.flush();
shuchuliu2.println("Cource:"+ziliao[k][j+2]);
shuchuliu2.flush();
shuchuliu2.println("Performance:"+ziliao[k][j+3]);
shuchuliu2.flush();
}
}
}
catch(Exception e){}
}}
class WinAdpt extends WindowAdapter //¹Ø±Õ¹¦ÄÜ
{
public void windowClosing(WindowEvent e)
{
((Frame)e.getWindow()).dispose();
System.exit(0);
}
}
模糊查询的 连接的时候有点问题。郁闷啊。
望哪位高手能帮小弟 解决一下。
服务器端
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class MyServer
{
static final int PORT=1119;
static final int MAX=5;
public static void main(String[] args) throws IOException,SocketException
{
ServerService Server=new ServerService(PORT,MAX);
}
}
class ServerService extends Frame implements Runnable
{
ServerSocket jianting;
TextArea state;
Thread thread;
Socket lianjie;
boolean temp1,temp2;
String a="0";
String[] xinxi=new String[3];
String[] xinxi1=new String[3];
String[][] ziliao={
{"25","shuai","java","98"},
{"26","billy","c","66"},
{"27","xiaochong","java","88"},
{"28","hacker","c","75"}
};
boolean[] linshi=new boolean[4];
public ServerService(int port,int max)
{
try
{
setTitle("·þÎñÆ÷¿ªÊ¼¼àÌý");
jianting=new ServerSocket(port,max);
setLayout(new BorderLayout());
state=new TextArea("Server starting...\n",20,50);
this.addWindowListener(new WinAdpt());
add("Center",state);
setResizable(false); //ÉèÖô°¿Ú´óС²»¿É±ä
pack(); //°´Ê×Ñ¡³ß´ç·ÅÖøô°¿ÚµÄ×Ó×é¼þ¡£
show();
while(true)
{
lianjie=jianting.accept();
InetAddress ClientAddress=lianjie.getInetAddress();
state.appendText("¿Í»§¶ËÁ¬½ÓÀ´×Ô:"+ClientAddress.toString()+"\n");
thread=new Thread(this);
thread.start();
//state.append("µ±Ç°¿Í»§¶Ë¹Ø±Õ!"+"\n");
}
}catch(IOException e){}
}
public void run()
{
try
{
DataInputStream shuruliu=new DataInputStream(new BufferedInputStream(lianjie.getInputStream()));
PrintStream shuchuliu=new PrintStream(new BufferedOutputStream(lianjie.getOutputStream()));
while(true)
{
String jie=shuruliu.readLine();
if(jie.equals("###"))
jqchaxun();
else if(jie.equals("***"))
mhchaxun();
else if(jie.equals("##"))
break;
}
state.append("µ±Ç°¿Í»§¶Ë¹Ø±Õ!"+"\n");
thread.destroy();
}
catch(Exception e){}
}
public void jqchaxun()
{
try
{
DataInputStream shuruliu1=new DataInputStream(new BufferedInputStream(lianjie.getInputStream()));
PrintStream shuchuliu1=new PrintStream(new BufferedOutputStream(lianjie.getOutputStream()));
for(int i=0;i<3;i++)
{
String jie=shuruliu1.readLine();
xinxi[i]=jie;
}
boolean temp=true;
for(int k=0,j=0;k<4;k++)
if(xinxi[0].equals(ziliao[k][j])&&xinxi[1].equals(ziliao[k][j+1])&&xinxi[2].equals(ziliao[k][j+2]))
{
temp=false;
shuchuliu1.println("Sno:"+ziliao[k][j]);
shuchuliu1.flush();
shuchuliu1.println("Name:"+ziliao[k][j+1]);
shuchuliu1.flush();
shuchuliu1.println("Cource:"+ziliao[k][j+2]);
shuchuliu1.flush();
shuchuliu1.println("Performance:"+ziliao[k][j+3]);
shuchuliu1.flush();
break;
}
if(temp)
{
shuchuliu1.println("#");
shuchuliu1.flush();
}
}
catch(Exception e){}
}
public void mhchaxun()
{
try
{
DataInputStream shuruliu2=new DataInputStream(new BufferedInputStream(lianjie.getInputStream()));
PrintStream shuchuliu2=new PrintStream(new BufferedOutputStream(lianjie.getOutputStream()));
for(int i=0;i<3;i++)
{
String jie=shuruliu2.readLine();
xinxi1[i]=jie;
}
for(int n=0;n<4;n++)
linshi[n]=false;
for(int m=0;m<4;m++)
//for(int i=0;i<1;i++)
for(int j=0;j<xinxi1[0].length();j++)
{
char linshi1=xinxi1[0].charAt(j);
if(ziliao[m][0].indexOf((int)linshi1)!=-1&&!linshi[m])
{
linshi[m]=true;
break;
}
}
for(int m=0;m<4;m++)
// for(int i=0;i<1;i++)
for(int j=0;j<xinxi1[1].length();j++)
{
char linshi1=xinxi1[1].charAt(j);
if(ziliao[m][1].indexOf((int)linshi1)!=-1&&!linshi[m])
{
linshi[m]=true;
break;
}
}
for(int m=0;m<4;m++)
// for(int i=0;i<1;i++)
for(int j=0;j<xinxi1[2].length();j++)
{
char linshi1=xinxi1[2].charAt(j);
if(ziliao[m][2].indexOf((int)linshi1)!=-1&&!linshi[m])
{
linshi[m]=true;
break;
}
}
for(int i=0,j=0,k=0;i<4;i++,k++)
{
if(linshi[i])
{
shuchuliu2.println("Sno:"+ziliao[k][j]);
shuchuliu2.flush();
shuchuliu2.println("Name:"+ziliao[k][j+1]);
shuchuliu2.flush();
shuchuliu2.println("Cource:"+ziliao[k][j+2]);
shuchuliu2.flush();
shuchuliu2.println("Performance:"+ziliao[k][j+3]);
shuchuliu2.flush();
}
}
}
catch(Exception e){}
}}
class WinAdpt extends WindowAdapter //¹Ø±Õ¹¦ÄÜ
{
public void windowClosing(WindowEvent e)
{
((Frame)e.getWindow()).dispose();
System.exit(0);
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class MSystem extends Applet implements ActionListener
{
Label label1,label2,label3,label4,label5,label;
TextField textfield1,textfield2,textfield3,textfield4,textfield5;
Button button1,button2,button3,button4,button5;
TextArea xianshi;
String host,xuehao,name,kecheng;
String[] sender=new String[3];
int port;
Connect socket;
JOptionPane Dialog;
public void init()
{
label=new Label("====================»¶ Ó­ µÇ ¼ ѧ Éú ³É ¼¨ ²é ѯ ϵ ͳ====================");
label1=new Label("·þÎñÆ÷IP/Ö÷»úÃû:");
label2=new Label("¶Ë¿Ú:");
label3=new Label("ѧºÅ:");
Dialog=new JOptionPane();
label3.setVisible(false);
label4=new Label("ÐÕÃû");
label4.setVisible(false);
label5=new Label("¿Î³ÌÃû:");
label5.setVisible(false);
textfield1=new TextField(9);
textfield2=new TextField(4);
textfield3=new TextField(9);
textfield3.setVisible(false);
textfield4=new TextField(5);
textfield4.setVisible(false);
textfield5=new TextField(6);
textfield5.setVisible(false);
button1=new Button("Á¬½Ó");
button2=new Button("¶Ï¿ª");
button3=new Button("¾«È·²éѯ");
button3.setVisible(false);
button4=new Button("Ä£ºý²éѯ");
button4.setVisible(false);
button5=new Button("Çå¿Õ");
button5.setVisible(false);
xianshi=new TextArea(10,40);
xianshi.setVisible(false);
//xianshi.enable(false);
//this.setBackground();
add(label);
add(label1);
add(textfield1);
add(label2);
add(textfield2);
add(button1);
add(button2);
add(label3);
add(textfield3);
add(label4);
add(textfield4);
add(label5);
add(textfield5);
add(button3);
add(button4);
add(button5);
add(xianshi);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
}
public void destroy()
{
socket.closeliu();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
host=textfield1.getText();
port=Integer.parseInt(textfield2.getText());
socket=new Connect(host,port);
if(socket.panduan)
Dialog.showMessageDialog(null,"ºÙºÙ!Á¬½Ó·þÎñÆ÷³É¹¦!");
//if(socket.panduan)
label3.setVisible(true);
label4.setVisible(true);
label5.setVisible(true);
textfield3.setVisible(true);
textfield4.setVisible(true);
textfield5.setVisible(true);
button3.setVisible(true);
button4.setVisible(true);
button5.setVisible(true);
xianshi.setVisible(true);
// }
}
else if(e.getSource()==button2)
{
socket.closeliu();
textfield1.setText("");
textfield2.setText("");
label3.setVisible(false);
label4.setVisible(false);
label5.setVisible(false);
textfield3.setText("");
textfield3.setVisible(false);
textfield4.setText("");
textfield4.setVisible(false);
textfield5.setText("");
textfield5.setVisible(false);
button3.setVisible(false);
button4.setVisible(false);
button5.setVisible(false);
xianshi.setText("");
xianshi.setVisible(false);
}
else if(e.getSource()==button3)
{
jqchaxun();
}
else if(e.getSource()==button4)
{
mhchaxun();
}
else if(e.getSource()==button5)
{
xianshi.setText("");
textfield3.setText("");
textfield4.setText("");
textfield5.setText("");
}
}
public void jqchaxun()
{
xuehao=textfield3.getText();
sender[0]=xuehao;
name=textfield4.getText();
sender[1]=name;
kecheng=textfield5.getText();
sender[2]=kecheng;
socket.send2();
socket.send(sender);
socket.receive();
if(socket.temp.equals("#"))
{
xianshi.setText("");
xianshi.append("¶Ô²»Æð!Êý¾Ý¿âÖÐûÓиÃѧÉú¼Ç¼!"+"\n");
}
else
{
xianshi.setText("¸ÃѧÉú³É¼¨ÈçÏÂ:\n");
for(int i=0;i<4;i++)
xianshi.append(socket.shuzu[i]+"\n");
}
}
public void mhchaxun()
{
xuehao=textfield3.getText();
sender[0]=xuehao;
name=textfield4.getText();
sender[1]=name;
kecheng=textfield5.getText();
sender[2]=kecheng;
socket.send3();
socket.send1(sender);
socket.receive1();
xianshi.setText("");
xianshi.append("¸ÃѧÉú³É¼¨ÈçÏÂ:\n");
for(int i=0;i<16;i++)
xianshi.append(socket.shuzu1[i]+"\n");
}}
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Connect
{
Socket ClientSocket;
PrintStream shuchuliu,scl;
DataInputStream shuruliu,srl;
String jieshou,xh,mingzi,course,jieshou1;
JOptionPane Dialog=new JOptionPane();
boolean panduan=true;
String[] shuzu=new String[4];
String[] shuzu1=new String[16];
String temp;
boolean temp1;
public Connect(String host,int duankou)
{
try
{
ClientSocket=new Socket(host,duankou);
shuchuliu=new PrintStream(new BufferedOutputStream(ClientSocket.getOutputStream()));
shuruliu=new DataInputStream(new BufferedInputStream(ClientSocket.getInputStream()));
scl=new PrintStream(new BufferedOutputStream(ClientSocket.getOutputStream()));
srl=new DataInputStream(new BufferedInputStream(ClientSocket.getInputStream()));
panduan=ClientSocket.isConnected();
//if(ClientSocket.isConnected())
//Dialog.showMessageDialog(null,"ºÙºÙ!Á¬½Ó·þÎñÆ÷³É¹¦!");
}
catch(Exception e){}
}
public void receive()
{
try
{
temp="";
for(int i=0;i<4;i++)
{
jieshou=shuruliu.readLine();
if(jieshou.equals("#"))
{
temp="#";
break;
}
shuzu[i]=jieshou;
}
}catch(IOException e){}
}
public void receive1()
{
try
{
for(int j=0;j<16;j++)
shuzu1[j]="";
for(int i=0;i<16;i++)
{
jieshou1=srl.readLine();
shuzu1[i]=jieshou1;
}
}
catch(IOException e){}
}
public void send(String aa[])
{
try
{
for(int i=0;i<3;i++)
{
shuchuliu.println(aa[i]);
shuchuliu.flush();
}
}
catch(Exception e){}
}
public void send1(String b[])
{
try
{
for(int i=0;i<3;i++)
{
scl.println(b[i]);
scl.flush();
}
}
catch(Exception e){}
}
public void send2()
{
shuchuliu.println("###");
shuchuliu.flush();
}
public void send3()
{
scl.println("***");
scl.flush();
}
public void closeliu()
{
try
{
scl.println("##");
scl.flush();
scl.close();
srl.close();
shuchuliu.close();
shuruliu.close();
ClientSocket.close();
}
catch(Exception e){}
}
}
applet网页
<html>
<body>
<title>::ѧÉú³É¼¨²éѯϵͳ::</title>
<applet code="MSystem.class" height="400" width="400"></applet>
</body>
</html>