Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Problem connecting socket to port

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Sandwich
    Posts
    14
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Problem connecting socket to port

    I keep getting an error when I try to connect my client to my server
    It times out and wont connect giving the standard IOException
    Server
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    public class Server{
    	@SuppressWarnings("deprecation")
    	public static void main(String args[]){
    		String line;
    		JFrame frame = new JFrame("Server");
    		JTextArea text = new JTextArea();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300,300);
    		frame.add(text);
    		frame.setVisible(true);
     
    		ServerSocket ss;
    		Socket       cs = null;
    		DataInputStream input;
    		DataOutputStream output;
     
    		try {
    			ss= new ServerSocket(2272);
    			text.append("Server Created\n");
    			text.append("Server Adress:"+ss.getInetAddress().toString()+"\n");
    			text.append("port:"+ss.getLocalPort()+"\n");
    			text.append("Waiting for connection\n");
    			cs = ss.accept();
    		} catch (IOException e) {
    			text.append("IOException\n");
    			e.printStackTrace();
    		}
    		if(cs.isConnected()){
    			text.append("Connected");
    		}
    	}
    }
    Client code
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
     
    public class clientOne implements ActionListener{
    	Socket clientSocket = null;
    	JFrame frame;
    	JTextArea ta;
    	JTextField tf;
    	JPanel jp;
    	JButton eb;
    	DataInputStream input = null;
    	DataOutputStream output;
    	public clientOne(){
    		eb = new JButton("Enter");
    		eb.addActionListener(this);
    		ta = new JTextArea();
    		ta.setBackground(Color.black);
    		ta.setForeground(Color.WHITE);
    		ta.setEditable(false);
    		tf = new JTextField();
    		tf.setBackground(Color.WHITE);
    		jp = new JPanel();
    		jp.setLayout(new BorderLayout());
    		jp.add(tf,BorderLayout.CENTER);
    		jp.add(eb,BorderLayout.EAST);
    		frame = new JFrame("clientOne");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300,300);
    		frame.setLayout(new BorderLayout());
    		frame.add(ta,BorderLayout.CENTER);
    		frame.add(jp,BorderLayout.SOUTH);
    		frame.setVisible(true);
    		connect();
    	}
    	private void connect(){
    		ta.append("Connecting\n");
    		try {
    			clientSocket=new Socket();
    			clientSocket.connect(new InetSocketAddress(ip, 2272));
    			ta.append("Connected\n");
    		} catch (UnknownHostException e) {
    			ta.append("Unkown Host\n");
    		} catch (SecurityException e) {
    			ta.append("Security error\n");
    		} catch (IOException e) {
    			ta.append("Some other error\n");
    			connect();
    		}catch (NullPointerException e){
    			ta.append("Address is null\n");
    		}
    	}
    	public static void main(String[] args){
    		new clientOne();
    	}
    	@Override
    	public void actionPerformed(ActionEvent evt) {
    		if(evt.getSource() == eb){
    				if(clientSocket!=null){
    					try {
    						output.writeChars(tf.getText());
    						output.close();
    					} catch (IOException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    				else{
    						System.out.println("error");
    					}			
    			}
    		}
    }
    When creating the socket I also have done
    myClient = new Socket(ip,port)
    If anyone could please help me trouble shoot i would be extremly grateful


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem connecting socket to port

    Thread moved.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem connecting socket to port

    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Location
    Sandwich
    Posts
    14
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Problem connecting socket to port

    java.net.ConnectException: Connection timed out: connect
    at java.net.TwoStacksPlainSocketImpl.socketConnect(Na tive Method)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress( Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at clientOne.connect(clientOne.java:53)
    at clientOne.<init>(clientOne.java:47)
    at clientOne.main(clientOne.java:67)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem connecting socket to port

    The posted code has compiler errors: definition of the variable: ip missing.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Location
    Sandwich
    Posts
    14
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Problem connecting socket to port

    So does that mean my ip is wrong?Also I have also tried the hostname instead of ip
    Last edited by Daryn; June 15th, 2014 at 04:51 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem connecting socket to port

    Try compiling the code that was posted. It has compiler errors that need to be fixed.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Nov 2012
    Location
    Sandwich
    Posts
    14
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Problem connecting socket to port

    cause i took out the ip and put in the word ip. But i replced ip with
    (new InetAdress.getLocalHost(),portNumber)

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem connecting socket to port

    That code does not compile. Please post the code you are using for testing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Cannot Open Socket on Specific Address/Port
    By dlamet in forum Java Networking
    Replies: 2
    Last Post: December 26th, 2011, 04:07 PM
  2. Replies: 12
    Last Post: September 3rd, 2011, 07:07 AM
  3. Creating a serverSocket while sonnecting to a socket on the same port
    By vortexnl in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2011, 01:33 PM
  4. Relay data from remote port to local port
    By chegers in forum Java Networking
    Replies: 0
    Last Post: November 7th, 2010, 12:46 PM
  5. Multiple socket on same port
    By Sanjith in forum Java Networking
    Replies: 2
    Last Post: October 22nd, 2010, 02:33 PM