Java – client – server network getting started

I am a good programmer, but I have no Internet experience

Basically, I want to enter the client server network For example, I want to try to get a server process that allows clients to connect over the Internet and send Ping to all other connected clients Then maybe I will try to develop a simple chat client or some simple multiplayer games, and I will go there

I know the language very well and may be useful: Java, C, C

How do I start? I want to learn best practices first, so good learning resources (such as books, online materials, etc.) you can recommend will be good

Editor: should I also study some kind of virtual machine to simulate the interaction between various machines?

Editor 2: I have offered a reward of 50 representatives So far, there are some good answers - I'm looking for more detailed answers, and I hope it will encourage me For example, it would be very helpful for people with this type of experience to compare the answers of different learning methods thank you! Can I still get some feedback on the whole virtual machine?

Solution

I prefer Java Let me explain TCP:

However, creating a server using java does this:

ServerSocket server = new ServerSocket(8888); // 8888 is the port the server will listen on.

"Socket" is the word you want to study, and you may look for it To connect your client to the server, you must write this:

Socket connectionToTheServer = new Socket("localhost",8888); // First param: server-address,Second: the port

But it's not connected yet The server must accept waiting clients (as I noted above):

Socket connectionToTheClient = server.accept();

Done! Your connection is established! Communication is like file-io The only thing you need to remember is that you have to decide when to flush the buffer and send data through the socket Using printstream for text writing is very convenient:

OutputStream out = yourSocketHere.getOutputStream();
PrintStream ps = new PrintStream(out,true); // Second param: auto-flush on write = true
ps.println("Hello,Other side of the connection!");
// Now,you don't have to flush it,because of the auto-flush flag we turned on.

Text reading of a BufferedReader is a good (best *) option:

InputStream in = yourSocketHere.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = br.readLine();
System.out.println(line); // Prints "Hello,Other side of the connection!",in this example (if this would be the other side of the connection.

I hope you can start from this information network! PS: of course, all network code must try ioexceptions

Editor: I forgot to write why it's not always the best choice BufferedReader uses buffers and reads as many buffers as possible But sometimes you don't want BufferedReader to steal bytes after line breaks and put them into its own buffer Short examples:

InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
// The other side says hello:
String text = br.readLine();
// For whatever reason,you want to read one single byte from the stream,// That single byte,just after the newline:
byte b = (byte) in.read();

But the BufferedReader already has this byte. You want to read it in its buffer So call in Read() will return the bytes following the last byte in the reader buffer

Therefore, in this case, the best solution is to use datainputstream and manage its own way to know how long the string will take, read only this number of bytes and convert it into a string Or: you use

DataInputStream.readLine()

This method does not use buffer, reads and checks line breaks byte by byte Therefore, this method will not steal bytes from the underlying InputStream

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>