# Socket sendet erst nach socket.close()



## tomtronic (29. Jul 2015)

Ich habe hier eine einfache bidirektionale Client-Server Verbindung.
Mein Problem besteht da drin das der Socket erst sendet nachdem er geschlossen wurde. Wenn ich den Socket nicht schließe, versendet er erst nachdem das Programm beendet wurde.
Hat jemand eine Idee?


```
package server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Server implements ActionListener {
   
    JFrame fenster;
    JPanel panel;
    JTextField eingabeFeld;
    JLabel label;
    JButton knopf;
   
    Vector<String> nachrichten;
    String stringSend;
    String stringReceive;
   
   
    ServerSocket server;
    ServerSocket s;
    Socket client;
   
    InputStream is;
    InputStreamReader isr;
    BufferedReader br;
   
    OutputStream os;
    OutputStreamWriter osw;
    BufferedWriter bw;
   
   
    Server()
    {
        fenster = new JFrame("Server");
        fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fenster.setPreferredSize(new Dimension(600,400));
        fenster.setLocation(1000,30);
       
        panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.yellow);
       
        label = new JLabel();
        label.setBackground(Color.yellow);
        label.setOpaque(true);

        eingabeFeld = new JTextField(30);
       
        knopf = new JButton("Senden");
        knopf.addActionListener(this);
       
        panel.add(eingabeFeld,BorderLayout.SOUTH);
        panel.add(label,BorderLayout.CENTER);
        panel.add(knopf, BorderLayout.EAST);
       
        fenster.add(panel);
       
        fenster.pack();
        fenster.setVisible(true);
       
        stringSend="";
        stringReceive="";
        nachrichten = new Vector<String>();
       
        try
        {
            server = new ServerSocket(1234);   
            client = server.accept();
            is = client.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
           
            while(true)
            {
                // Lesen   
                stringReceive = br.readLine();
                if (stringReceive != null)
                {
                    System.out.println(stringReceive);
                    label.setText(stringReceive);   
                }
            }
        }
        catch (IOException ex) { System.out.println("Server konnte nicht gestartet werden");}
           
       
           
    }
   
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
           
            stringSend = eingabeFeld.getText();
            eingabeFeld.setText("");
           
            // Schreiben
            os = client.getOutputStream();
            osw = new OutputStreamWriter(os);
            bw = new BufferedWriter(osw);
            bw.write(stringSend);
            bw.flush();
           
            client.close();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }
   
   
   
    public static void main(String[] args) {
        new Server();
    }

   
   
}
```




```
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Client implements ActionListener {
   
    JFrame fenster;
    JPanel panel;
    JTextField eingabeFeld;
    JLabel label;
    JButton knopf;
   
    Vector<String> nachrichten;
    String stringSend;
    String stringReceive;
   
    Socket client;
    Socket s;
   
    OutputStream os;
    OutputStreamWriter osw;
    BufferedWriter bw;
   
    InputStream is;
    InputStreamReader isr;
    BufferedReader br;
   
    int i=0;
   
    Client()
    {
        fenster = new JFrame("Client");
        fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fenster.setPreferredSize(new Dimension(600,400));
        fenster.setLocation(30,30);
       
        panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.yellow);
       
        label = new JLabel();
        label.setBackground(Color.yellow);
        label.setOpaque(true);

        eingabeFeld = new JTextField(30);
       
        knopf = new JButton("Senden");
        knopf.addActionListener(this);
       
        panel.add(eingabeFeld,BorderLayout.SOUTH);
        panel.add(label,BorderLayout.CENTER);
        panel.add(knopf, BorderLayout.EAST);
       
        fenster.add(panel);
       
        fenster.pack();
        fenster.setVisible(true);
       
        stringSend="";
        stringReceive="";
        nachrichten = new Vector<String>();
        try
        {
           
            while(true){
                // Lesen
                client = new Socket("0",1234);
                is = client.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);
                stringReceive = br.readLine();
                if (stringReceive != null)
                {
                    System.out.println(stringReceive);
                    label.setText(stringReceive);
                }
                client.close();       
            }
        }
        catch (IOException ex){System.out.println("Verbindung zu Server nicht möglich");}
       
    }
   
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            stringSend = eingabeFeld.getText();
            eingabeFeld.setText("");
           
            os = client.getOutputStream();
            osw = new OutputStreamWriter(os);
            bw = new BufferedWriter(osw);
            bw.write(stringSend);
            bw.flush();
                       
            //client.close();
        }
        catch (IOException ex) {Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);}
    }
   
   
   
    public static void main(String[] argv)
    {
        new Client();
    }

   
}
```


----------



## VfL_Freak (29. Jul 2015)

Moin,

habe mir den ganzen Code jetzt nicht angetan, aber vlt. hilft Dir dies ja weiter:
http://www.java-forum.org/thema/buf...socket-outputstream-was-gesendet-wird.107897/

Gruß Klaus


----------



## fhoffmann (29. Jul 2015)

Du ruft im Konstruktor von Client eine blockierende Methode auf (br.readLine()); der Konstruktor ist also noch gar nicht beendet bevor der Client eine Nachricht vom Server erhalten hat. Und von einem nicht fertig erstellten Objekt kannst du keine Methode (actionPerformed) aufrufen.


----------

