2 Replies - 641 Views - Last Post: 07 October 2012 - 09:14 AM Rate Topic: -----

#1 bladevetren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 18-August 12

ServerSocket error

Posted 07 October 2012 - 07:42 AM

hello, i have been attempting to create a java application which generates a server and exports a file to it which can then be retrieved by a client.the problem which is occuring is that my server applcation gui seems to crash whenever i attempt to export the file. therefore how may i fix this problem.
here is the code:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class server extends JFrame{
	private static JTextField serverportTF,documentTF;
	private static JLabel serverportL,documentL;
	private JButton export;
	public  server(){
		setLayout(new FlowLayout());
		serverportL=new JLabel("serverport");
		serverportTF=new JTextField(10);
		documentL= new JLabel("document");
		documentTF= new JTextField(10);
		export=new JButton("export");
		export e = new export();
		setTitle("server data");
	public class export  implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			int serverdata= new Integer(serverportTF.getText());
			ServerSocket serverSocket = new ServerSocket(serverdata);
            Socket socket = serverSocket.accept();
            System.out.println("Accepted connection : " + socket);
            File transferFile = new File (documentTF.getText());
            byte [] bytearray  = new byte [(int)transferFile.length()];
            FileInputStream fin = new FileInputStream(transferFile);
            BufferedInputStream bin = new BufferedInputStream(fin);
            OutputStream os = socket.getOutputStream();
            System.out.println("Sending Files...");
            System.out.println("File transfer complete");

			  } catch (Exception k1) {
	public static void main(String [] args){
		server gui = new server();

Is This A Good Question/Topic? 0
  • +

Replies To: ServerSocket error

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1575
  • View blog
  • Posts: 3,542
  • Joined: 05-April 11

Re: ServerSocket error

Posted 07 October 2012 - 08:13 AM

Please start your class names with an uppercase letter

Your gui stops responding because you are doing too much work on the event dispatch thread

When your actionPerformed method is called it will be on the event dispatch thread, so if you stop the thread from processing other events your gui will stop responding

So how to fix the problem? Just start your server socket in a new thread
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3534
  • View blog
  • Posts: 16,016
  • Joined: 20-September 08

Re: ServerSocket error

Posted 07 October 2012 - 09:14 AM

That's right, but it's not quite as simple as CasiOo, has suggested, since the Swing recommended practice is not only that processing-intensive tasks be done off the EDT but that updates to the gui be done on it. Since you will want to react with the gui (a finished app won't print file transfer status to the console, but to the gui) you will need to ensure that the correct thread is used for each bit of functionality. I recommend you study (through the excellent API docs) SwingWorker and use it in this case.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1