Answer the question
In order to leave comments, you need to log in
A few questions about client-server connection?
Hello. I am working on a client-server application project. I write the server in JAVA, and the client in FreePascal. The application scheme is not complicated. We enter the data, send it to the server, process it there, send the response back, display the result on the screen. There are no problems with algorithms and code for input, output and processing. There is a transmission problem. I am 0 in this question. I read several articles about sockets, but I only understood how to create sockets, and then everything is vague, but how to work with them is not clear at all.
Hence the questions:
Answer the question
In order to leave comments, you need to log in
1. There are several options, but all of them boil down to the fact that text, form data, etc. must be represented as a set of bytes. This is called serialization. This can be done in different ways. For example, use a binary format close to how this data is stored in memory. This is quite difficult, especially if the programs are written in different languages. A more high-level JSON-based (ndjson) or msgpack approach will work for you. Or even higher level solutions like json/msgpack rpc.
2. Reception works in reverse order. We accept data in some format and deserialize it into the data structures that are used in the program.
3. Clarify the question, but the answer is most likely no =)
4. He may or may not participate. It works on top of sockets. It's probably overkill for your purposes. However, you can look towards rpc over http or rest api.
5. The main problem is that the operation - "wait for a message from the client / server" is blocking. And if nothing is done, then the client will not be able to respond to the keyboard / mouse, and the server will not be able to work with multiple clients. Therefore, network communication can be moved to a separate thread or some asynchronous mechanisms can be used (in Pascal, this is not very similar).
6. Pascal may not be the best language for the client. On the same Java it would be easier.
In short, it is better to write a RESTfull api server for such data, and then you will send post/get/put/delete http requests from the client and the server will process them and send the results back.
1. You also need to understand that what kind of data flow, then such is the request. If you write an outputStreamData stream in freePascal, and specify inputStreamFile on the server, then the packet will not reach the output.
The principle is something like this:
Field [Name]: Alexander
Field [Last name]: Petrenko
Field [Age]: 11
Checkbox [Pioneer]: Yes (well, or a tick)
Query string:
[name] Александр;
[family] Петренко;
[age] 11;
[pioner] true;
To understand the principles, make it as simple as possible. For example:
1) Represent your data structure with exact sequences. Let's say you have data like "surname", "first name", "age", "married". The types will be string, string, int, boolean. 4 data fields.
2) Now you need to serialize your structure. You translate everything ("last name", "first name", "age", "married") into bytes.
3) On the client you connect to the socket server, the connection opens. You take the first field "surname" (remember that you already have bytes), you count its size (let's say it turned out 17 bytes). You translate the number (int) 17 into bytes. You write the byte number 17 to the server socket. Then you write all your 17 bytes of data there. You repeat this with all the data.
4) The server is running and waiting for a connection. The client has connected, the server starts waiting for data. Remember that in p1. We already know the data structure. You start reading int bytes from the socket. I read the number 17. You read 17 bytes from the socket, the "surname" field turned out, again you read from the socket int bytes. I learned from it how many bytes you need to read and you read them. Thus, you do 4 times (see paragraph 1). Now it remains to translate the data from bytes to string, string, int, boolean and you will get "last name", "first name", "age", "married" on the server.
5) The server disconnects the client or, let's say, sends a response according to the same principle.
Everything written above is very crooked, but to understand how data transfer over the network works, that's it.
To stop being afraid of sockets and understand them, imagine that these are ordinary files that you open, write and read.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question