-
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());
}
}
}
-
Re: Socket communication
Quote:
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?
-
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
-
Re: Socket communication
-
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.
-
Re: Socket communication
Added printStackTrace(),but i didn't get any errors.
-
Re: Socket communication
Please post the code and wrap it in code tags to preserve the formatting.
BB Code List - Java Programming Forum
-
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.