Java套接字/服务器编程:未连接套接字

时间:2016-11-25 20:19:43

标签: java sockets server

我最近一直试图用Java进行服务器编程,我想创建一个简单的控制台服务器交互。

我遇到的问题是,无论何时服务器运行,它总是崩溃,主要错误是套接字异常。

以下是代码:

SOK_CLIENT课程:

import java.io.*;
import java.net.*;

public class SOK_CLIENT
{
public static void main(String[] args) throws Exception
{
SOK_SERVER SERVER = new SOK_SERVER();
SOK_CLIENT CLIENT = new SOK_CLIENT();
CLIENT.run();
}
public void run() throws Exception
{
Socket SOCK = new Socket("localhost", 1025);
PrintStream PS = new PrintStream(SOCK.getOutputStream());
PS.println("Hello to Server From Client");

InputStreamReader IR = new InputStreamReader(SOCK.getInputStream());
BufferedReader BR = new BufferedReader(IR);

String MESSAGE = BR.readLine();
System.out.println(MESSAGE);
}
}

这是SOK_SERVER类:(错误所在的位置)

import java.io.*;
import java.net.*;

public class SOK_SERVER
{
  public SOK_SERVER()
{
try 
{
  run();
}
catch(Exception e) 
{
  e.printStackTrace();
}
}
public void run() throws Exception 
{
int PORT = 1025;
ServerSocket SRVSOCKET = new ServerSocket(PORT);
Socket SOCK = new Socket();
System.out.println("Server Starting at Port: " + PORT);
InputStreamReader IR = new InputStreamReader(SOCK.getInputStream());
BufferedReader BR = new BufferedReader(IR);
String MESSAGE = BR.readLine();
System.out.println(MESSAGE);
if(MESSAGE != null)
{
PrintStream PS = new PrintStream(SOCK.getOutputStream());
PS.println("Message Recieved");
}
}
}

例外:

java.net.SocketException: Socket is not connected
at java.net.Socket.getInputStream(Socket.java:796)
at SOK_SERVER.run(SOK_SERVER.java:31)
at SOK_SERVER.<init>(SOK_SERVER.java:16)
at SOK_CLIENT.main(SOK_CLIENT.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:362)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleMethodCall(ExpressionEvaluator.java:92)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84)
at koala.dynamicjava.tree.StaticMethodCall.acceptVisitor(StaticMethodCall.java:121)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695

2 个答案:

答案 0 :(得分:2)

您应该将服务器代码中的std::u16string更改为Socket SOCK = new Socket();

答案 1 :(得分:-1)

尝试

Socket SOCKET = new Socket();

SOCKET = SRVSOCKET.accept();

实现Runnable类和实例化为Thread

并调用start()方法..而不是run()

SOCK_CLIENT implements Runnable
SOCK_SERVER implements Runnable

在您的主要方法

SOK_SERVER SERVER = new SOK_SERVER();
SOK_CLIENT CLIENT = new SOK_CLIENT();
Thread t1 = new Thread(SERVER);
Thread t2 = new Thread(CLIENT);

t1.start();
t2.start();

从SOCK_SERVER构造函数中删除run();