这是我的客户:的Java通信客户端/服务器
public static void logIn(String name, String pass) {
try {
Socket s = new Socket("localhost", 9000);
PrintStream out = new PrintStream(s.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out.print(name + "," + pass);
out.flush();
out.close();
System.out.println(in.readLine());
in.close();
s.close();
}
catch(UnknownHostException exp)
{
exp.printStackTrace();
}
catch(java.io.IOException exp)
{
exp.printStackTrace();
}
}
,这我在我的服务器:
public static void main(String[] args){
boolean clientExists=false;
ArrayList<User> users = new ArrayList<User>();
users.add(new User("jmeno","heslo"));
ServerSocket ss;
try {
ss = new ServerSocket(9000);
while(true) {
clientExists=false;
Socket s = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream out = new PrintStream(s.getOutputStream());
String xmlpacket="";
String pom;
while ((pom = in.readLine())!=null){
xmlpacket+=pom;
xmlpacket+="\n";
}
for(User us: users)
{
if(us.isUserNameAndPasswordRight(login, passwd))
{
out.print("user is connected");
out.flush();
clientExists=true;
}
}
}
if(clientExist != true)
out.print("bad login");
out.flush();
out.close();
in.close();
s.close();
}
catch(java.io.IOException exp)
{
System.out.println("chyba u socketu!");
}
}
它更多钞票,以这样的工作?我不能得到这个工作,因为在客户端尝试从服务器读取答案时出现异常。
编辑: 这是堆栈跟踪:
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at client.client.logIn(client.java:79)
at client.GUI.GUI.jMenuItem1ActionPerformed(GUI.java:379)
at client.GUI.GUI.access$5(GUI.java:367)
at client.GUI.GUI$5.actionPerformed(GUI.java:151)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
请提交例外加上堆栈跟踪和突出问题的方法/线路 –