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
Client codeimport 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"); } } }
When creating the socket I also have doneimport 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"); } } } }If anyone could please help me trouble shoot i would be extremly gratefulmyClient = new Socket(ip,port)