Results 1 to 3 of 3
- 02-25-2011, 03:23 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 19
- Rep Power
- 0
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"
chatter.java to make list of chatters ips and portsimport 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();
}
}
}
}
and thread to control the chattersimport java.net.InetAddress;
public class chatter {
InetAddress ip;
int port;
public chatter(InetAddress ip, int port){
this.ip = ip;
this.port = port;
}
}
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! :(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());
}
}
}
- 02-25-2011, 03:43 PM #2
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
You deserve a kick in the nuts for naming the class "srver."
- 02-25-2011, 05:13 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Simple RMI chat application
By usmangt in forum New To JavaReplies: 3Last Post: 09-27-2010, 07:37 AM -
Simple TCP chat application not working for me
By sundarrajan in forum New To JavaReplies: 0Last Post: 08-13-2009, 08:31 AM -
simple chat program
By munishmhr in forum NetworkingReplies: 2Last Post: 03-25-2009, 04:00 PM -
simple chat server
By sari in forum New To JavaReplies: 0Last Post: 02-06-2009, 02:30 AM -
Simple serverless chat solution
By goodjonx in forum NetworkingReplies: 3Last Post: 01-07-2008, 03:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks