java.net.SocketException: Connection reset

0
23 янв 2016 21:41
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class Connection {

	private static Socket connection;
	private static ObjectOutputStream out;
	public static Thread connectThread, sendData;

	public void initConnection() {
		connectThread = new Thread(new Runnable() {
			public void run() {
				while (true) {
					try {
						connection = new Socket(InetAddress.getByName("127.0.0.1"), 2802);
						out = new ObjectOutputStream(connection.getOutputStream());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});
	}

	public void sendData(final Object obj) {
		sendData = new Thread(new Runnable() {
			public void run() {
				try {
					out.flush();
					out.writeObject(obj);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		sendData.start();
	}

	public Thread getConnectThread() {
		return connectThread;
	}

	public void setConnectThread(Thread connectThread) {
		this.connectThread = connectThread;
	}

	public Socket getConnection() {
		return connection;
	}

	public void setConnection(Socket connection) {
		Connection.connection = connection;
	}

	public ObjectOutputStream getOut() {
		return out;
	}

	public void setOut(ObjectOutputStream out) {
		Connection.out = out;
	}
}

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	private static ServerSocket server;
	private static Socket connection;
	private static Thread serverThread, inputData, outputData;
	private static ObjectInputStream input;

	public static void main(String args[]) {
		startServer();
	}

	private static void startServer() {
		serverThread = new Thread(new Runnable() {
			public void run() {
				try {
					server = new ServerSocket(2802);
				} catch (IOException e) {
					e.printStackTrace();
				}
				while (true) {
					try {
						connection = server.accept();
						input = new ObjectInputStream(connection.getInputStream());
						System.out.println(input.readObject());
					} catch (IOException | ClassNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
		}, "StartServer");
		serverThread.start();
	}

	public static ServerSocket getServer() {
		return server;
	}

	public static void setServer(ServerSocket server) {
		Server.server = server;
	}

	public static Socket getConnection() {
		return connection;
	}

	public static void setConnection(Socket connection) {
		Server.connection = connection;
	}

	public static Thread getServerThread() {
		return serverThread;
	}

	public static void setServerThread(Thread serverThread) {
		Server.serverThread = serverThread;
	}

}

во время попытки отправки объекта вылетает стек
java.net.SocketException: Connection reset
	at java.net.SocketInputStream.read(Unknown Source)
	at java.net.SocketInputStream.read(Unknown Source)
	at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
	at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
	at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
	at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
	at java.io.ObjectInputStream.<init>(Unknown Source)
	at chat_server.Server$1.run(Server.java:30)
	at java.lang.Thread.run(Unknown Source)

Ответов: 1

0
12 янв 2017 00:39
Модераторы: Нет
Сейчас эту тему просматривают: Нет