How to transmit data between the client and server

Source: Internet
Author: User
I feel very confused about network transmission. I did not want the materials I checked online, so I had to come here to ask for help! Recently, I am working on a chat room. The client is android and I have built workerman locally as a server. Both sides adopt the websocket protocol .... I feel very confused about network transmission. I did not want the materials I checked online, so I had to come here to ask for help!

Recently, I am working on a chat room. The client is android and I have built workerman locally as a server. Both sides adopt the websocket protocol.

I use inputstream to read txt files and send them to the server through websocket. After the server receives the data, it writes the txt file. The txt file can be opened normally.

Now, when I use the same method to read the. amr file recorded by android and send it to the server and write it into the. amr file, the file becomes corrupted and cannot be opened.

Based on this, I feel that I am focusing only on the implementation of software functions, but not on computer principles. There are some issues that you think are naive:
1: How does one transmit data between networks?
I read the audio files and convert them into binary files and transmit them to the server. How can the server restore these binary files to their original files?

2: How can I solve the problem that files cannot be opened due to file corruption described above?

Or is my understanding wrong at the beginning, and I cannot read the audio file? How can I convert an audio file to transfer it?

Android client code:

 mConnection.connect(wsuri, new WebSocketHandler() { @Override public void onOpen() { Log.d(TAG, "Status: Connected to " + wsuri); InputStream is = null; try {         is = new FileInputStream(_file);     } catch (FileNotFoundException e) {         e.printStackTrace();     } byte[] bytes = new byte[1024]; int len = 0; try {        while((len=is.read(bytes))!=-1)        {            Log.d(TAG, "senBinaryMessage: " + bytes);            mConnection.sendBinaryMessage(bytes);        }        is.close();     } catch (IOException e) {        e.printStackTrace();        }                       }                

}

Server code:

$ Worker-> onMessage = function ($ connection, $ data) {$ filePath = "/Users/myname/Desktop/php/"; if (! File_exists ($ filePath) {// if the specified folder does not exist, create the folder mkdir ($ filePath, 0777);} $ name = $ filePath. 'Voice '. '. amr'; $ fp = fopen ($ name, "a"); if (fwrite ($ fp, $ data) {echo "template written successfully ";} else {fclose ($ fp); echo "failed to Write template! ";}};

Reply content:

I feel very confused about network transmission. I did not want the materials I checked online, so I had to come here to ask for help!

Recently, I am working on a chat room. The client is android and I have built workerman locally as a server. Both sides adopt the websocket protocol.

I use inputstream to read txt files and send them to the server through websocket. After the server receives the data, it writes the txt file. The txt file can be opened normally.

Now, when I use the same method to read the. amr file recorded by android and send it to the server and write it into the. amr file, the file becomes corrupted and cannot be opened.

Based on this, I feel that I am focusing only on the implementation of software functions, but not on computer principles. There are some issues that you think are naive:
1: How does one transmit data between networks?
I read the audio files and convert them into binary files and transmit them to the server. How can the server restore these binary files to their original files?

2: How can I solve the problem that files cannot be opened due to file corruption described above?

Or is my understanding wrong at the beginning, and I cannot read the audio file? How can I convert an audio file to transfer it?

Android client code:

 mConnection.connect(wsuri, new WebSocketHandler() { @Override public void onOpen() { Log.d(TAG, "Status: Connected to " + wsuri); InputStream is = null; try {         is = new FileInputStream(_file);     } catch (FileNotFoundException e) {         e.printStackTrace();     } byte[] bytes = new byte[1024]; int len = 0; try {        while((len=is.read(bytes))!=-1)        {            Log.d(TAG, "senBinaryMessage: " + bytes);            mConnection.sendBinaryMessage(bytes);        }        is.close();     } catch (IOException e) {        e.printStackTrace();        }                       }                

}

Server code:

$ Worker-> onMessage = function ($ connection, $ data) {$ filePath = "/Users/myname/Desktop/php/"; if (! File_exists ($ filePath) {// if the specified folder does not exist, create the folder mkdir ($ filePath, 0777);} $ name = $ filePath. 'Voice '. '. amr'; $ fp = fopen ($ name, "a"); if (fwrite ($ fp, $ data) {echo "template written successfully ";} else {fclose ($ fp); echo "failed to Write template! ";}};

TCP working mode:
Https://zh.wikipedia.org/wiki/%E4%BC%A0%E8%BE%93%E6%8E%A7%E5%88%B6%E5%...

I think there are two possible causes for file corruption.
I. fopen is not usedbMark

Https://secure.php.net/manual/zh/function.fopen.php
If the 'B' flag is not specified when operating the binary file, some strange problems may occur, including bad image files and strange problems about \ r \ n characters.

Solution:
Add when opening the filebMark

phpfopen($name,"ab")

2. The file is not closed after the write is successful.
This may cause:
1. Data will stay in the buffer zone, and data will not be written to the file until the buffer zone is full or when the program exits or garbage collection is disabled. (I am too clear about whether php will recycle files)
2. The order of files closed during program exit or garbage collection may be random, which will lead to inconsistent data order.
Use python for a test

python3a = open('test', 'ab')a.write(b'123')b = open('test', 'ab')b.write(b'456')b.close()a.close()

The content of the test file is:

456123

Solution:
Manually close files

phpIf (fwrite ($ fp, $ data) {echo "template written successfully";} else {echo "failed to Write template! ";} Fclose ($ fp );

Or use

phpfflush($fp);

To write data into the buffer.

I did not perform a test because I am not familiar with php.

We recommend that you use the http api because the socket is too low-layer and many blocking problems cannot be solved. You can create a web api on the server side. Android provides excellent http encapsulation, search for the httpclient class library of apache.

First, generally, network communication is based on TCP/IP, and both http and socket are encapsulated. The following communication protocol stacks are maintained by the operating system. Generally, there is no problem.
The second problem is that socket-based network communication still has links and no links. This file can be uploaded completely. Datainputstream and others are supported. I have simulated an FTP server and it has nothing to do with the file format. We recommend that you check whether the client file has been completely transmitted to the server.

The server code is incorrect. The file should be appended.

$ Worker-> onMessage = function ($ connection, $ data) {$ filePath = "/Users/myname/Desktop/php/"; if (! File_exists ($ filePath) {// if the specified folder does not exist, create the folder mkdir ($ filePath, 0777);} $ name = $ filePath. 'Voice '. '. amr'; // === the file to be written in the append mode === file_put_contents ($ filePath. $ name, $ data, FILE_APPEND );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.