懂JAVA和DELPHI SOCKET编程的人请进(200分)

  • 主题发起人 主题发起人 alt_wf
  • 开始时间 开始时间
A

alt_wf

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在用delphi编写了一个客户端程序,但是当给服务端发送数据的时候,服务端总是
第一次能正常的接受,再次发送的时候就不能正常的接受了,如果发送多次服务端程序
也能偶尔接受到发送的信息,我用DELPHI编写了一个服务端程序接受都是正常的但是用
JAVA服务端程序却出现了以上问题,请问各位高手这个问题如何解决呢?是否和JAVA的
线程有关!盼回复!JAVA服务端部分程序如下:
class TLoginThread extends Thread {
private Socket sSocket = null;

public TLoginThread(Socket accept_Socket) {
super("TLoginThread");
this.sSocket = accept_Socket;
}

public void Login() {
InputStream Is = null;
OutputStream Os = null;
byte[] bIn = new byte[256];
byte[] bOut = new byte[256];
TReadCfg ReadCfg = new TReadCfg();
String url = ReadCfg.strURL;
String str;
//等待接收用户
try {
Is = sSocket.getInputStream();
Os = sSocket.getOutputStream();
int iNumBytes = Is.read(bIn, 0, 256);
str = new String(bIn);
System.out.println(str.trim());
if (str.trim().equals("login")) {
//要求登录
Is.read(bIn, 0, 256); //等待读入用户名
String strUser_Name = new String(bIn);
System.out.println(strUser_Name.trim());
Is.read(bIn, 0, 256); //等待读入用户口令
String strUser_Password = new String(bIn);
System.out.println(strUser_Password.trim());
System.out.println('进入了登陆系统');
try {
try {
Class.forName(ReadCfg.DBdrv);
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

Connection con =
DriverManager.getConnection(
url,
ReadCfg.DBuser,
ReadCfg.DBpassword);
Connection con_update =
DriverManager.getConnection(
url,
ReadCfg.DBuser,
ReadCfg.DBpassword);

PreparedStatement pstmt_User =
con.prepareStatement(
"select User_ID from TMsg_User where User_Name = ? and User_Password = ?");
pstmt_User.setString(1, strUser_Name.trim());
pstmt_User.setString(2, strUser_Password.trim());
ResultSet rs_User = pstmt_User.executeQuery();

if (rs_User.next()) {
String strUser_ID = rs_User.getString("User_ID");
//得到用户ID
//验证成功
PreparedStatement pstmt_Update =
con_update.prepareStatement(
"update TMsg_User set Status = ?, User_IP = ? where User_Name = ? and User_Password = ?");
pstmt_Update.setInt(1, 1); //更新状态
String strIP =
sSocket.getInetAddress().getHostAddress();
pstmt_Update.setString(2, strIP.trim());
pstmt_Update.setString(3, strUser_Name.trim());
pstmt_Update.setString(4, strUser_Password.trim());
pstmt_Update.executeUpdate();
pstmt_Update.close();

System.out.println("User " + strUser_Name.trim() + " login seccess");
str = "login_success";
bOut = str.getBytes();
Os.write(bOut, 0, bOut.length); //发送登录成功

send_Msg(strUser_ID); //发送消息给注册用户

} else {
str = "login_error";
bOut = str.getBytes();
Os.write(bOut, 0, bOut.length); //发送登录失败
}
rs_User.close();
pstmt_User.close();
con.close();
con_update.close();

} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
} //try end

} else if (str.trim().equals("logout")) {
//用户离线
Is.read(bIn, 0, 256); //等待读入用户名
String strUser_Name = new String(bIn);
Is.read(bIn, 0, 256); //等待读入用户口令
String strUser_Password = new String(bIn);

try {
Connection con =
DriverManager.getConnection(
url,
ReadCfg.DBuser,
ReadCfg.DBpassword);
Connection con_update =
DriverManager.getConnection(
url,
ReadCfg.DBuser,
ReadCfg.DBpassword);

PreparedStatement pstmt_User =
con.prepareStatement(
"select * from TMsg_User where User_Name = ? and User_Password = ?");
pstmt_User.setString(1, strUser_Name.trim());
pstmt_User.setString(2, strUser_Password.trim());
ResultSet rs_User = pstmt_User.executeQuery();
if (rs_User.next()) {
//验证成功
PreparedStatement pstmt_Update =
con_update.prepareStatement(
"update TMsg_User set Status = ? where User_Name = ? and User_Password = ?");
pstmt_Update.setInt(1, 0); //更新状态
pstmt_Update.setString(2, strUser_Name.trim());
pstmt_Update.setString(3, strUser_Password.trim());
pstmt_Update.executeUpdate();
pstmt_Update.close();
System.out.println("User "+ strUser_Name.trim()+ " logout seccess");
str = "logout_success";
bOut = str.getBytes();
Os.write(bOut, 0, bOut.length); //发送离线成功
} else {
str = "logout_error";
bOut = str.getBytes();
Os.write(bOut, 0, bOut.length); //发送离线失败
}
rs_User.close();
pstmt_User.close();
con.close();
con_update.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
} //try end

}
} catch (Exception e) {
System.out.println("Error:" + e);
}

}
}
 
后退
顶部