读书人

socket编程!高手帮忙找一下那出有关问

发布时间: 2012-02-03 22:02:47 作者: rapoo

socket编程!高手帮忙找一下那出问题了。急!100分!急
客户端提交的内容后,没法收到服务端发回的信息。
客户端程序:
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;


public class Database_client extends Applet implements Runnable ,ActionListener
{
Button b1,b2,b3;Label a,b,c,d,e,f;Panel p1,p2;TextField text,bb,cc,dd,ee,ff;
Socket scoket=null;
DataInputStream in = null;
DataOutputStream out = null;
Thread thread;
public void init()
{//生成界面;
//setLayout(new BorderLayout());
b1=new Button( "提交 ");b2=new Button( "清除 ");b3=new Button( "退出 ");
a=new Label( "请你仔细核对所填项的数据后提交! ");
b=new Label( "用户账号 ");c=new Label( "用户密码 ");
d=new Label( "转入金额 ");e=new Label( "转入户账 ");
//f=new Label( "注意 ");
bb=new TextField(16);ee=new TextField(16);
dd=new TextField(16);cc=new TextField(16);
text=new TextField(20);
add(a);
add(b);add(bb);
add(c);add(cc);
add(d);add(dd);add(e);add(ee);
add(b1);add(b2);add(b3);add(text);
b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);

}
public void start()
{
try{
scoket=new Socket(this.getCodeBase().getHost(),4331);
in=new DataInputStream(scoket.getInputStream());
out=new DataOutputStream(scoket.getOutputStream());
}
catch(IOException e){}
if(thread==null)
{
thread=new Thread(this);
//thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}

}

public void run()
{

String s=null;
while(true)
{
try
{
s=in.readUTF();
text.setText(s);
}
catch(IOException e)
{
System.out.println( "与服务器已断开! ");
text.setText( "与服务器已断开! ");
break;
}

}
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1)
{//需要把
String m=null;;//把文本框的内容都防入s中 用”,“做分界
String s1=bb.getText();String s2=cc.getText();
String s3=dd.getText();String s4=ee.getText();
m=s1+ ", "+s2+ ", "+s3+ ", "+s4;
if(m!=null)
{
try
{
out.writeUTF(m);
//out.flush();
}
catch(IOException ee){}
}

}
else if (e.getSource()==b2)
{
bb.setText(null);cc.setText(null);
dd.setText(null);ee.setText(null);

}
else if (e.getSource()==b3)
{
System.exit(0);
}
}
}


服务器端程序:

import java.io.*;
import java.net.*;
//import java.util.*;
import java.sql.*;
import java.util.StringTokenizer;


public class Database_server
{public static void main(String args[])
{
ServerSocket server=null;

Socket you=null;

while (true)
{


try
{
server = new ServerSocket(4331);
}
catch(IOException e)
{
System.out.println( "正在监听 ");
}
try
{
you=server.accept();//(Database_server.java:27)

System.out.println( "客户端的地址: "+you.getInetAddress());
}
catch(IOException e)
{
System.out.println( "正在等待客户: ");
}
if(you != null)
{
new Server_thread(you).start();
}
else{continue;}
}

}
}

class Server_thread extends Thread
{
Socket socket;
Connection con=null;
Statement stmt=null;
DataOutputStream out=null;
DataInputStream in=null;
String s=null;Float M;int i=0;
Server_thread(Socket t)
{
socket =t;
try
{
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}

}
public void run()
{


while(true)
{ String a[]=new String[3];
for(int i=0;i <4;i++) a[i]=null;
try{int i=0;
s=in.readUTF();
StringTokenizer f=new StringTokenizer(s, ", ");
while(f.hasMoreElements()){a[i]=f.nextToken();i++;}//获取的内容到数组a[i]中

try
{
M=Float.valueOf(a[2]).floatValue();
}
catch(NumberFormatException e){}

String sql1= "select * from sys_table where 卡号=? and 密码=? ";
PreparedStatement ps1=con.prepareStatement(sql1);
ps1.setString(1, a[0]);
ps1.setString(2, a[1]);
ResultSet rs=ps1.executeQuery();
while(rs.next())
{float yu_e1=rs.getFloat( "余额 ");
if((yu_e1 <M)&&(yu_e1==M))
{
try{out.writeUTF( "账户余额不足请冲值 ");}
catch(IOException ex)
{
System.out.println( "dsdsd ");
}

break;
}

else {
String s= " ' "+a[3]+ " ' ";
String sql2= "select * from sys_table where 卡号=? ";
PreparedStatement ps2=con.prepareStatement(sql2);
ps2.setString(1, s);
ResultSet rs2=ps2.executeQuery();
while(rs2.next())
{
float yu_e2=rs2.getFloat( "余额 ");


float y1=yu_e1-M;float y2=yu_e2+M;

String sql3= "updata user_table set 余额= "+y1+ " where 卡号= "+a[0].trim();
stmt.executeUpdate(sql3);
String sql4= "updata user_table set 余额= "+y2+ " where 卡号= "+a[3].trim();
stmt.executeUpdate(sql4);
try{
out.writeUTF( "转帐成功 ");
}
catch(IOException ex)
{
System.out.println( "oooooooo ");
}

System.out.println( "转帐成功 ");
break;
}
}sleep(100);
}
}catch(Exception ex){}

}
}
}



[解决办法]
String a[]=new String[3];
for(int i=0;i <4;i++) a[i]=null;

越界 !
i <3
[解决办法]
好长啊 帮顶下
[解决办法]
只看了服务器端的!serversocket you没有得到连接的时候,一直是空的!但是你没有捕捉异常!
嘿嘿!!

[解决办法]
Applet
为什么继承它呀,这不是小应用程序吗。
[解决办法]
太长,只能大概看:
server = new ServerSocket(4331);
这个放到你那个while(true)循环之外,服务器端只要创建一次,然后监听客户端消息。
[解决办法]
补充一下:你现在是循环创建server对象,同时一次循环完毕没有关闭通讯端口,当然有问题。
[解决办法]
String a[]=new String[3];
for(int i=0;i <4;i++) a[i]=null; // 数组越界 i <3;

public class Database_server
{
public static void main(String args[])
{
ServerSocket server=null;

Socket you=null;
try
{
server = new ServerSocket(4331);
}catch(IOException e){System.out.println( "正在监听 ");}

while(true)
{
try
{
you=server.accept();//(Database_server.java:27)

System.out.println( "客户端的地址: "+you.getInetAddress());
}catch(IOException e){System.out.println( "正在等待客户: ");}
if(you != null)
new Server_thread(you).start();
else
continue;
}

}
}

[解决办法]
是的,就是数组越界啊!

[解决办法]
up

读书人网 >J2SE开发

热点推荐