Socket problem

mrno

Erfahrenes Mitglied
Hi,
der Socket schließt zu früh was kann ich da gegen machen.
Mein code:
Code:
package Fehler;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


import java.io.ObjectOutputStream;

public class Test extends Thread implements ActionListener{
	JFrame f = new JFrame("Test");
	JTextField ipTF,clientportTF,serverPortTF,messageTF;
	SendData sendData=null;
	public Test(){
		f.setSize(100,50);
		f.setLayout(new GridLayout(2,2));
		ipTF = new JTextField("");
		f.add(ipTF);
		clientportTF = new JTextField();
		f.add(clientportTF);
		serverPortTF = new JTextField();
		serverPortTF.addActionListener(this);
		f.add(serverPortTF);
		messageTF = new JTextField();
		messageTF.addActionListener(this);
		f.add(messageTF);
		f.setVisible(true);
		sendData = new SendData(6889);
		sendData.start();
	}

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

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==serverPortTF){
			sendData.stop();
			sendData=new SendData(Integer.parseInt(serverPortTF.getText()));
			sendData.start();
		}else{
			Socket socket;
			try {
				socket = new Socket(ipTF.getText(),Integer.parseInt(clientportTF.getText()));
			ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
			oos.writeObject(messageTF.getText());
			oos.flush();
			oos.close();
			ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
			Object o=ois.readObject();
			System.out.println(o);
			ois.close();
			socket.close();
			} catch (NumberFormatException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
class SendData extends Thread{
	int port=6889;
	public SendData(int port){
		this.port=port;
	}

	public void run(){
		try {
			ServerSocket s = new ServerSocket(port);
			Socket socket=s.accept();
			ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
			Object o=ois.readObject();
			ois.close();
			System.out.println(o);
			ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
			oos.writeObject("Bekommene nachricht:"+o);
			oos.flush();
			oos.close();
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
Fehlermeldung vom Client
Code:
java.net.SocketException: Socket is closed
	at java.net.Socket.getInputStream(Socket.java:762)
	at Fehler.Test.actionPerformed(Test.java:69)
	at javax.swing.JTextField.fireActionPerformed(JTextField.java:487)
	at javax.swing.JTextField.postActionEvent(JTextField.java:668)
	at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:783)
	at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1571)
	at javax.swing.JComponent.processKeyBinding(JComponent.java:2763)
	at javax.swing.JComponent.processKeyBindings(JComponent.java:2798)
	at javax.swing.JComponent.processKeyEvent(JComponent.java:2726)
	at java.awt.Component.processEvent(Component.java:5265)
	at java.awt.Container.processEvent(Container.java:1966)
	at java.awt.Component.dispatchEventImpl(Component.java:3955)
	at java.awt.Container.dispatchEventImpl(Container.java:2024)
	at java.awt.Component.dispatchEvent(Component.java:3803)
	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1810)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:672)
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:920)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:798)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:636)
	at java.awt.Component.dispatchEventImpl(Component.java:3841)
	at java.awt.Container.dispatchEventImpl(Container.java:2024)
	at java.awt.Window.dispatchEventImpl(Window.java:1774)
	at java.awt.Component.dispatchEvent(Component.java:3803)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Code:
cool
java.net.SocketException: Socket is closed
	at java.net.Socket.getOutputStream(Socket.java:799)
	at Fehler.SendData.run(Test.java:104)
das cool ist der text den ich gesendet habe. Er kommt also beim server an nur das request vom server geht nicht. Was mache ich falsch?
 
Zurück