Results 1 to 3 of 3
  1. #1
    Ruuhkis is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default Can it be that hard to make simple chat?

    I've been trying to make a simple chat that takes many clients at time and send the message to everyone everytime but something wents always wrong! This is like my 10th try but still no result.

    this is the main class "srver.java"
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    import java.util.List;


    public class srver {
    List<chatter> chatters = new ArrayList<chatter>();
    public static void main(String args[]){
    srver s = new srver();
    }
    public srver(){
    while(true){
    DatagramSocket socket = null;
    try {
    socket = new DatagramSocket(1234);
    } catch (SocketException e) {
    e.printStackTrace();
    }
    byte[] buf = new byte[256];
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    try {
    socket.receive(packet);
    } catch (IOException e) {
    e.printStackTrace();
    }
    InetAddress address = packet.getAddress();
    int port = packet.getPort();
    chatters.add(new chatter(address, port));
    new chatServerThread(socket, address, port, this).start();
    }
    }
    public void sendAll(String message){
    for(chatter x: chatters){
    DatagramSocket socket = null;
    try {
    socket = new DatagramSocket();
    } catch (SocketException e) {
    e.printStackTrace();
    }
    byte[] buf = new byte[256];
    buf = message.getBytes();
    DatagramPacket paketti = new DatagramPacket(buf, buf.length, x.ip, x.port);
    try {
    socket.send(paketti);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    chatter.java to make list of chatters ips and ports
    import java.net.InetAddress;
    public class chatter {
    InetAddress ip;
    int port;
    public chatter(InetAddress ip, int port){
    this.ip = ip;
    this.port = port;
    }
    }
    and thread to control the chatters
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;


    public class chatServerThread extends Thread {
    DatagramSocket s;
    InetAddress ip;
    int port;
    srver server;
    public chatServerThread(DatagramSocket s, InetAddress ip, int port, srver server){
    this.s = s;
    this.ip = ip;
    this.port = port;
    this.server = server;
    }
    public void run(){
    while(s.isConnected()){
    byte[] buf = new byte[254];
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    try {
    s.receive(packet);
    } catch (IOException e) {
    e.printStackTrace();
    }
    server.sendAll(packet.toString());
    }
    }
    }
    when the server gets client connected it just shuts down and i dont find the reason why? Can anyone improve my ways of handling the clients? I know google is nice buddy but doesnt work this time for me! :(

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    You deserve a kick in the nuts for naming the class "srver."

  3. #3
    Ruuhkis is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    Mate, server class was already in use on project i made it on.

Similar Threads

  1. Simple RMI chat application
    By usmangt in forum New To Java
    Replies: 3
    Last Post: 09-27-2010, 07:37 AM
  2. Simple TCP chat application not working for me
    By sundarrajan in forum New To Java
    Replies: 0
    Last Post: 08-13-2009, 08:31 AM
  3. simple chat program
    By munishmhr in forum Networking
    Replies: 2
    Last Post: 03-25-2009, 04:00 PM
  4. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 02:30 AM
  5. Simple serverless chat solution
    By goodjonx in forum Networking
    Replies: 3
    Last Post: 01-07-2008, 03:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •