package ChatRoom;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class ChatRoomServer {
String username;
String message = "";
List<Chatter> chatters = new ArrayList<Chatter>();
public void go() {
try {
PartyLine partyLine = new PartyLine();
ServerSocket socket = new ServerSocket(8989);
while (true) {
Socket sock = socket.accept();
Chatter chatter = new Chatter(sock, partyLine);
chatter.start();
chatters.add(chatter);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
ChatRoomServer server = new ChatRoomServer();
server.go();
}
class PartyLine {
public void broadcast(String username, String message) {
for(Chatter chatter : chatters) {
// if(!chatter.username.equals(username)) {
chatter.broadcast(username, message);
// }
}
}
}
class Chatter extends Thread {
private final Socket socket;
private final PartyLine partyLine;
private String username;
private PrintWriter writer;
Chatter(Socket socket, PartyLine partyLine) {
this.socket = socket;
this.partyLine = partyLine;
}
public void run() {
try {
writer = new PrintWriter(socket.getOutputStream());
InputStreamReader streamReader = new InputStreamReader(socket
.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
writer.print("Username: ");
writer.flush();
username = reader.readLine();
String message = "";
while (!message.equals("exit")) {
if(!message.equals("")) {
partyLine.broadcast(username, message);
}
writer.print("<" + username + ">");
writer.flush();
message = reader.readLine();
}
writer.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void broadcast(String username, String message) {
writer.println(username + ": " + message);
writer.flush();
}
}
}
Sponsored by: █ Sparkhost - Hosting Without Compromises! █ Hybrid Performance Web Hosting █ Spark Host Stream Hosting █ Hybrid IRC & IRCd Server Shell Accounts
New To Socket Programming
Started by
blacksage
, Dec 18 2007 04:34 PM
3 replies to this topic
#1
Posted 18 December 2007 - 04:34 PM
I just started programming with sockets and wrote a little program that sets up a server and waits for a client(s) to join. To connect to the server I just did telnet 192.168.0.201 4141. My question is can I connect to the server outside of my network?
#2
Posted 19 December 2007 - 10:09 AM
chan!!
You need to read some basic networking...
If you are going to be able to connect from outside your network or not, isn't defined by your socket.
That's defined by your network configuration, Do you have a firewall? are you behind a NAT?
Drop your firewall, port foward the desired port in your nat and you can connect from wherever you want.
You need to read some basic networking...
If you are going to be able to connect from outside your network or not, isn't defined by your socket.
That's defined by your network configuration, Do you have a firewall? are you behind a NAT?
Drop your firewall, port foward the desired port in your nat and you can connect from wherever you want.
#3
Posted 18 February 2008 - 02:57 PM
Thanks APX, I just got it to work. After I droped the firewall and setup port forwarding in my router itchan!!
You need to read some basic networking...
If you are going to be able to connect from outside your network or not, isn't defined by your socket.
That's defined by your network configuration, Do you have a firewall? are you behind a NAT?
Drop your firewall, port foward the desired port in your nat and you can connect from wherever you want.
worked.
#4
Posted 09 May 2008 - 08:50 AM
Did it work with multipul users? If i do remember correctly you have to cast your chatter object as a Thread to use start wthout blocking. Just maby, its been a while sence i have programmed in java.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












