Java socket programming

Discussion in 'Java' started by Wasps, Dec 30, 2017.

  1. Wasps


    Dec 30, 2017
    Likes Received:
    I am trying to program a chat application between server and client using sockets.
    I followed some tutorials and tried to modify. However, the code does not seem to work.
    I am trying to get the port number from a text field when the connect button is clicked and establish the connection. After the button is clicked the button does not work and the program goes into infinite loop.
    Can anyone help??
    Here is the code:
    For server:

    Code (Java):

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Server extends JFrame{
       public JFrame frame;
       public JButton sendJButton;
       public JPanel main;
       public JPanel right;
       public JPanel panel;
       private JScrollPane pane;
       private Container window;
       private JTextField userText;
       private JTextArea chatWindow;
       private ObjectOutputStream output;
       private ObjectInputStream input;
       private ServerSocket server;
       private Socket connection;
       private JTextField port;
       private JLabel portLabel;
       private JLabel chooseServer;
       private JLabel ip;
       private JTextField ipAdd;
       public static int portNum;
       public JButton connect;
       public Server() {
           frame= new JFrame();
           panel=new JPanel();
           main=new JPanel();
           right=new JPanel();
           String choice[]={"TCP","UDP"};

           final JComboBox<String> opt = new JComboBox<String>(choice);
           sendJButton=new JButton("Send");
           connect=new JButton("Connect");
           userText=new JTextField();
           chatWindow=new JTextArea();
           port=new JTextField();
           chooseServer=new JLabel("Server:");
           ip=new JLabel("IP Address");
           ipAdd=new JTextField();
           portLabel=new JLabel("Port:");
           pane=new JScrollPane(chatWindow);
           panel.setPreferredSize(new Dimension(600, 550));
           main.setPreferredSize(new Dimension(450, 550));
           right.setPreferredSize(new Dimension(150, 550));
           portLabel.setBounds(15, 90, 30, 30);
                   new ActionListener() {
                       public void actionPerformed(ActionEvent event) {
                           // TODO Auto-generated method stub
           panel.setLayout(new BorderLayout());
           panel.add(main, BorderLayout.WEST);
           panel.add(right, BorderLayout.EAST);
           connect.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   // TODO Auto-generated method stub
       public void setPort(){
       public void startRunning(){
               server=new ServerSocket(portNum,100);
                   }catch(EOFException eofException){
                       showMessage("\n Server ended the connection");
           }catch(IOException ioException){
       private void waitForConnection() throws IOException{
           showMessage("\n Waiting for someone to connect...");
           showMessage("\n Now connected to "+connection.getInetAddress().getHostName());

       private void setupStreams() throws IOException{
           output = new ObjectOutputStream(connection.getOutputStream());
           input= new ObjectInputStream(connection.getInputStream());
           showMessage("\n Streams are now set up");
       private void whileChatting() throws IOException{
           String message="You are now connected";
                   message = (String) input.readObject();
               }catch(ClassNotFoundException classNotFoundException){
                   showMessage("\n I dont know what the user sent");
           }while(!message.equals("CLIENT - END"));
       private void closeAll(){
           showMessage("\n Closing connection... \n");
           }catch(IOException ioException){
       private void sendMessage(String message){
           try {
               output.writeObject("SERVER - "+message);
               showMessage("\n SERVER - "+message);
           }catch(IOException ioException) {
               chatWindow.append("\n ERROR: Message not sent");
       private void showMessage(final String text){
               new Runnable(){
                   public void run(){
       private void ableToType(final boolean tof){
               new Runnable(){
                   public void run(){
    ServerTest class:

    Code (Java):

    import javax.swing.*;
    public class ServerTest {
    public static void main(String[] args) {
       Server obj=new Server();

    Last edited by a moderator: Dec 31, 2017
    Wasps, Dec 30, 2017
    1. Advertisements

  2. Wasps

    John Joe

    Apr 25, 2017
    Likes Received:
    You have while(true) in your code,thats why your program goes into infinite loop
    John Joe, Jan 1, 2018
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.