Results 1 to 8 of 8
  1. #1
    0248991 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Socket communication

    I want to create chat with one client and server side program,but it doesn't work.If you have any suggestion,please reply them.

    Client side:

    import java.awt.Button;
    import java.awt.FlowLayout;
    import java.awt.TextArea;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;

    import javax.swing.JFrame;


    public class client extends JFrame
    {
    static Socket sktclient;
    static ObjectOutputStream oos;
    static BufferedReader in;
    static boolean pripojeni=false;
    static TextArea chatokno;
    TextField zprava;

    public static void main(String[] args) throws IOException, ClassNotFoundException
    {
    client a=new client();
    a.setVisible(true);
    for(int i=1;i>0;i+=0)
    {
    if(pripojeni==true)
    {
    pripojeni=false;
    i=-5;
    getchatokno().setText("Connected to:"+sktclient.getRemoteSocketAddress());
    }
    }
    oos = new ObjectOutputStream(sktclient.getOutputStream());
    in =new BufferedReader(new InputStreamReader(sktclient.getInputStream()));
    Thread t=new Thread();
    t.run();

    }
    public void run() throws IOException, ClassNotFoundException
    {
    while (in.readLine() != null)
    {
    chatokno.setText(chatokno.getText()+"\n"+in.readLi ne());

    }
    }
    public void setsocket(Socket a)
    {
    this.sktclient=a;
    pripojeni=true;
    }
    public static TextArea getchatokno()
    {
    return chatokno;
    }
    public client()
    {
    super.setTitle("M.H.P.L - MegaHustodemonska hra po lanu");
    this.setLayout(new FlowLayout());
    this.setSize(600,600);
    final TextField ip;
    final TextField port;
    chatokno=new TextArea("",200,50);
    Button nastavit,odeslat;
    ip=new TextField("192.168.1.34");
    port=new TextField("6222");
    nastavit=new Button("Set");
    nastavit.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    try
    {
    Socket pokusnastaveni=new Socket(ip.getText(),Integer.parseInt(port.getText( )));
    setsocket(pokusnastaveni);
    }
    catch (NumberFormatException e1)
    {} catch (UnknownHostException e1)
    {} catch (IOException e1)
    {}
    }
    });
    zprava=new TextField("message to send");
    odeslat=new Button("Odeslat");
    odeslat.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    try
    {
    oos.writeUTF(zprava.getText());
    }
    catch (IOException e1)
    {}
    }
    });
    this.add(ip);
    this.add(port);
    this.add(nastavit);
    this.add(zprava);
    this.add(odeslat);
    this.add(chatokno);

    }
    }


    Server side:

    import java.awt.Button;
    import java.awt.FlowLayout;
    import java.awt.TextArea;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;

    import javax.swing.JFrame;


    public class server extends JFrame
    {

    TextField zprava;
    static BufferedReader in;
    static ObjectOutputStream oos;
    Button odeslat;
    static TextArea chatokno;
    static ServerSocket serversocket;
    static Socket serverovna;
    static boolean pripojeni;
    static int naslouchaciport=6222;
    public server() throws IOException
    {
    super.setTitle("M.H.P.L - MegaHustodemonska hra po lanu");
    this.setLayout(new FlowLayout());
    this.setSize(600,600);
    serversocket=new ServerSocket(naslouchaciport);
    zprava=new TextField("zde pis zpravy");
    odeslat=new Button("Odeslat");
    chatokno=new TextArea("",50,75);
    pripojeni=false;
    this.add(zprava);
    this.add(odeslat);
    odeslat.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    try
    {
    oos.writeUTF(zprava.getText());
    }
    catch (IOException e1)
    {}
    }
    });
    this.add(chatokno);
    }
    public static TextArea getchatokno()
    {
    return chatokno;
    }
    public static ServerSocket getsocket()
    {
    return serversocket;
    }
    public static int getnaslouchaciport()
    {
    return naslouchaciport;
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException
    {
    server a=new server();
    a.setVisible(true);
    Socket serverovna;
    while(true)
    {
    serverovna=serversocket.accept();
    chatokno.setText("Connected to:"+serverovna.getRemoteSocketAddress());
    Thread t=new Thread();
    in =new BufferedReader(new InputStreamReader(serverovna.getInputStream()));
    oos =new ObjectOutputStream(serverovna.getOutputStream());
    t.run();
    }
    }
    public void run() throws IOException, ClassNotFoundException
    {
    while (in.readLine() != null)
    {
    chatokno.setText(chatokno.getText()+"\n"+in.readLi ne());

    }
    }

    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Socket communication

    but it doesn't work
    I recommend you define the above, as this tells us absolutely nothing. Are there exceptions? Does it compile? If it misbehaves, how does it misbehave?

  3. #3
    0248991 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Socket communication

    It can be compiled.After ip and port of the server are set with button on client side,last what it can do is write on both sides,where are sockets connected.
    I found that serversocket listening on port where client is trying to connect creates new Socket (socket=serversocket.accept();),but this new one has different port than client socket.somewhere i found,that new socket must be created,but dont know on which side ,or wow to set variables

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Socket communication

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Socket communication

    There are a lot of empty catch blocks that are ignoring potential errors. Add calls to the printStackTrace() method to all of the catch blocks in case there is an exception being ignored.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    0248991 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Socket communication

    Added printStackTrace(),but i didn't get any errors.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Socket communication

    Please post the code and wrap it in code tags to preserve the formatting.
    BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    0248991 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Socket communication

    I have solved it!!!Problem was in thread.I forgot Runnable and for method run() it didn't work.So I just added Runnable and it works great.
    But thank you for your time.

Similar Threads

  1. Socket communication error
    By robertmacedonia in forum Networking
    Replies: 5
    Last Post: 09-07-2010, 11:09 AM
  2. communication with socket
    By yacin87 in forum Networking
    Replies: 0
    Last Post: 04-08-2010, 11:14 PM
  3. Replies: 5
    Last Post: 11-01-2009, 05:16 PM
  4. Replies: 2
    Last Post: 09-10-2009, 02:29 PM
  5. Socket Communication on different networks
    By ersachinjain in forum Networking
    Replies: 4
    Last Post: 09-05-2009, 02:32 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
  •