I want to write a simple chat on Java sockets without multithreading.
1. I don’t really understand how a socket connection with another computer happens, if by transferring an ip socket, then how to make a default connection with dynamic ip.
2. Which input / output classes to use.
5 answers
We need a client and a server, clients connect to the server, and it transmits the received messages to all connected clients. server part:
public class Server { static ServerSocket socket; public static List<ConnectedClient> clients = new ArrayList<>(); public static void main(String[] args) throws IOException { socket = new ServerSocket(2000); while (true) { ConnectedClient client = new ConnectedClient(socket.accept()); clients.add(client); client.start(); } } } public class ConnectedClient extends Thread { private Socket sock; private BufferedReader input; private PrintWriter output; public ConnectedClient(Socket s) throws IOException { sock = s; System.out.println("new user connected from " + s.getInetAddress().toString()); input = new BufferedReader(new InputStreamReader(sock.getInputStream())); output = new PrintWriter(sock.getOutputStream()); } @Override public void run() { try { while (sock.isConnected()) { String readed = input.readLine(); for (CinnectedClient c : server.clients) { c.send(readed); } } } catch (IOException e) { System.out.println(e.toString()); } finally { System.out.println("user disconnected from " + sock.getInetAddress().toString()); } } public void send(String s) throws IOException { output.println(s); output.flush(); } }
client part: socket for connecting to the server:
Socket s = new Socket(<IP сервера>, <порт подключения>);
similarly to the server, take the InputStream and OutputStream from the socket:
s.getInputStream() s.getOutputStream()
and with their help we read and write data
the string sent to the OutputStream will come in the InputStream to the sender and all other clients connected to the server.
You first decide on the chat architecture:
- peer-to-peer - that is, when the comp. connects to a computer directly without an intermediary, for example, BitTorrent is so organized
- client-server, when there is a centralized server and all communication goes through it, an example is Skype, ICQ
- Mixed system - an example of Yahoo Messenger
- in general, Skype was still almost pure p2p. And only with the advent of Microsoft, he gradually centralized, but still he is now mixed and closer to p2p. - KoVadim
- I did not know - for some reason he always thought that he was pure centralized. - Barmaley
"client-server, when there is a centralized server and all communication goes through it, an example of Skype, ICQ."
Skype is not implemented for the client-server in terms of the exchange of temporary data (messages, audio / video stream). On the Skype server, only user data is stored.
Take into account, when there are 3 participants in the call, the leader of the call is announced and all data goes through it. He becomes a server. Chat - if you write to a person offline and exit, then that person will not receive this message until the first one is online.
- need a server to which these dynamic will connect
- what do you know?)
Look for the best library for working with sockets, I once found, but it was quite a while, so I won’t say exactly the name. They are not many, so do not get lost)