Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By Norm

Thread: Simplest Server socket example behaves like a Congressman

  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default Simplest Server socket example behaves like a Congressman

    I am trying to create the simplest possible server socket app to test my "real" app. However, with the code below I'm sometimes getting "Could not listen on port: 4211" and other times it simply shuts down (console says "<terminated> ..."). And I never see the GUI - it terminates before it even shows up.

    Why would this be? What would prevent it from listening on that port, and how can I get it to stay awake? Except for the minimal GUI code, this is taken almost verbatim from the "official" tutorial here: Reading from and Writing to a Socket (The Java™ Tutorials > Custom Networking > All About Sockets)

    This is my code:

    Java Code:
    import java.net.*;
    import java.awt.BorderLayout;
    import java.io.*;
    import javax.swing.*;
    
    public class ServSockTest {
    
    	public static void main(String[] args) throws IOException {
    		new ServSockTest().go();
    	}
    	
    	public void go() throws IOException {
    		// GUI
    		JTextArea jta;
    		JFrame frame = new JFrame("Server Socket Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel mainPanel = new JPanel();
    		jta = new JTextArea(12, 50);
    		jta.setLineWrap(true);
    		jta.setWrapStyleWord(true);
    		jta.setEditable(false); 
    		jta.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    		JScrollPane qScrollerSent = new JScrollPane(jta);
    		qScrollerSent
    				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    		qScrollerSent
    				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		
    		ServerSocket serverSocket = null;
    		try {
    		    serverSocket = new ServerSocket(4211);
    		} catch (IOException e) {
                                                        jta.append("Could not listen on port: 4211");
    		    System.exit(-1);
    		}
    		
    		mainPanel.add(jta);
    		frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
    		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
    		frame.setSize(630, 840);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    		//< GUI
    
    		Socket clientSocket = null;
    		try {
    		    clientSocket = serverSocket.accept();
    		} catch (IOException e) {
                                                        jta.append("Accept failed: 4211");
    		    System.exit(-1);
    		}
    
    		PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    		        BufferedReader in = new BufferedReader(
    						new InputStreamReader(
    						clientSocket.getInputStream()));
    		        String inputLine, outputLine;
    		        out.println("Hey!");
    
    		        while ((inputLine = in.readLine()) != null) {
    		             outputLine = String.format("I just received: %s",  in);
                                                 jta.append(outputLine);
    		             out.println(outputLine);
    		             if (outputLine.equals("Bye."))
    		                break;
    		        }
    		        out.close();
    		        in.close();
    		        clientSocket.close();
    		        serverSocket.close();
    		    }
    	}
    Last edited by blackbird; 07-18-2011 at 09:51 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I haven't gone through all of your code, but one issue is that your "minimal GUI code" completely ignores threading concerns.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default

    What exception is thrown? Your code doesn't show it.
    Add a call to printStackTrace to your catch clause to see what the error is?

    Also remove the System.exit() calls if you want to see anything on the GUI.
    Last edited by Norm; 07-19-2011 at 01:01 AM.
    blackbird likes this.

  4. #4
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I haven't gone through all of your code, but one issue is that your "minimal GUI code" completely ignores threading concerns.
    What should it do?

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default

    Put the background code (socket stuff) on its own thread.
    blackbird likes this.

Similar Threads

  1. Socket HTTP-Server
    By MichaelH in forum Networking
    Replies: 6
    Last Post: 05-06-2011, 08:45 PM
  2. non-blocking SSL socket server
    By e_scape in forum Networking
    Replies: 0
    Last Post: 04-12-2011, 05:18 PM
  3. events on a server socket
    By newbiejava in forum New To Java
    Replies: 13
    Last Post: 08-03-2010, 08:24 AM
  4. Server Socket
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 09-05-2009, 06:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •