咋就不对劲呢? 咋就不对劲呢?
这两个程序的功能是:客户机与服务器相连后,在客户机上输入用户名和密码并按“登录”按钮,服务器收到用户名及密码并返回“恭喜你!你已登录成功。”但我运行时竟出不来效果,问题出在哪儿呢?请各位给我个答案吧!谢谢!谢谢!!客户端:
import javax.swing.*;
import *;
import *;
import java.awt.event.*;
import java.awt.*;
public class Client extends JApplet
{ JLabel usename;
JLabel password;
JTextField user;
JPasswordField pass;
JButton login;
JPanel p;
GridBagLayout layout;
GridBagConstraints con;
public void init()
{ usename = new JLabel("用户名:");
password =new JLabel("密码:");
user =new JTextField(10);
pass =new JPasswordField(10);
login=new JButton("登录");
layout=new GridBagLayout();
con =new GridBagConstraints();
p=(JPanel)getContentPane();
p.add(usename);
p.add(user);
p.add(password);
p.add(pass);
p.add(login);
p.setLayout(layout);
con.anchor=GridBagConstraints.WEST;
con.gridx=1;
con.gridy=1;
layout.setConstraints(usename,con);
con.gridy=2;
layout.setConstraints(user,con);
con.gridy=3;
layout.setConstraints(password,con);
con.gridy=4;
layout.setConstraints(pass,con);
con.anchor=GridBagConstraints.CENTER;
con.gridx=1;
con.gridy=5;
layout.setConstraints(login,con);
}
}
class panel extends Client
{ public void init()
{
loginAction log=new loginAction();
login.addActionListener(log);
}
class loginAction implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ String message;
Socket client;
if(e.getSource()==login)
{
try
{message=user.getText()+":"+pass.getPassword();
client=new Socket(InetAddress.getLocalHost(),8080);
//getAppletContext().showStatus("客户开始!");
DataOutputStream toServer = new DataOutputStream(client.getOutputStream());
DataInputStream fromServer = new DataInputStream(client.getInputStream());
toServer.writeUTF(message);
getAppletContext().showStatus((String)fromServer.readUTF());
user.setText(null) ;
pass.setText(null);
toServer.close();
fromServer.close();
}
catch(IOException et)
{getAppletContext().showStatus("客户不能发送到服务器!"+":"+et);}
}
}
}
}
服务器端:
import java.util.*;
import *;
import *;
public class createServer
{ ServerSocket linkSocket=null;
Socket netSocket=null;
public createServer()
{ while(true)
{ try
{ linkSocket=new ServerSocket(8080);
netSocket=linkSocket.accept();
doServer serverThread=new doServer(netSocket);
serverThread.start();
}
catch(IOException e1)
{ System.out.println( e1.getMessage());
}
}
}
public static void main(String args[])
{new createServer();}
}
class doServer extends Thread
{ Socket netSocket;
DataInputStream fromClient;
DataOutputStream toClient;
doServer(Socket netSocket)
{
try
{
DataInputStream fromClient= new DataInputStream(netSocket.getInputStream());
DataOutputStream toClient = new DataOutputStream(netSocket.getOutputStream());
}
catch(IOException e2)
{System.out.println(e2.getMessage());}
}
public void run()
{ String message=null;
try
{ while(true)
{ message=fromClient.readUTF();}
}
catch(IOException e3)
{System.out.println(e3.getMessage());}
try{
toClient.writeUTF("恭喜你!你已登录成功。");}
catch(IOException e4)
{System.out.println(e4.getMessage());}
System.out.println(message);
try{
netSocket.close();
fromClient.close();
toClient.close();
}
catch(IOException e5)
{System.out.println(e5.getMessage());}
}
}