Home > Software engineering >  Why to send a OK before you file transfer information
Why to send a OK before you file transfer information

Time:09-15

I saw any file transfer program, the receiver before transfer files to send an OK message, the sender information after received the OK to send, I can't get rid of the code to send and receive OK information fully transfer files? Here is my sample program, we can analysis the is why
The sender
 # include "stdafx. H" 
#include
#include
#include
# pragma comment (lib, "ws2_32. Lib")
Int main () {
WSADATA wsaData;
If (WSAStartup (MAKEWORD (2, 2), & amp; WsaData))//initialize WinSock protocol stack
{cout<& lt;" Winsock cannot be initialized!" ;
WSACleanup ();
return 0;
}
The SOCKET sockSer sockConn;//note that the server must be created two socket
SockSer=socket (AF_INET SOCK_STREAM, 0).//initialize the socket
SOCKADDR_IN addrSer addrCli;//note that the server to create two socket address
AddrSer. Sin_family=AF_INET;
AddrSer. Sin_port=htons (5566);
AddrSer. Sin_addr. S_un. S_addr=inet_addr (" 127.0.0.1 ");

Bind (sockSer, (SOCKADDR *) & amp; AddrSer, sizeof (SOCKADDR));//bind socket
Listen (sockSer, 5);

Int len=sizeof (SOCKADDR);
Cout<& lt;" Waiting for the client's servers... "
SockConn=accept (sockSer, (SOCKADDR *) & amp; AddrCli, & amp; Len);//to accept connections, pay attention to the return value
If (sockConn==INVALID_SOCKET) {
Cout<& lt;" The server to accept client connections fail!" return 0; }
The else cout<& lt;" Server accepts a client connection success!"
Char fileName [256]="C: \ \ cf. JPG";

Char filebuf [1000], OK [3], fsize [50].
/open to transfer the file * * * * * */
The FILE * fp.//create a file pointer
If ((fp=fopen (fileName, "rb"))==NULL) {//byte in read-only mode opens the file
Cout & lt; <"Can't open the file:" & lt; Closesocket (sockConn);//close the socket, the other side waiting for the recv () function returns 0
Closesocket (sockSer); WSACleanup ();
return 0; }//file open failed exit is
/* * * * * * access to the file length/
The fseek (fp, 0 l, SEEK_END);//the location of the file pointer to the end of the file
Long int size=ftell (fp);//get the current file pointer position value, the value is the length of the file
The fseek (fp, 0 l, SEEK_SET);//the location of the file pointer to the file beginning
Long int fileSize=size;
//fileSize=htonl (size);//the file length in the structure of variable fileMsg
Itoa (fileSize, fsize, 10);
Send (sockConn, fsize, strlen (fsize) + 1, 0).//send a fileName
Cout/* * * to receive the other party sent OK information * * */
If (recv (sockConn, OK, sizeof (OK), 0) & lt;=0) {
Cout & lt; <"Receive OK to fail, program exits! \ n ";
Closesocket (sockSer); WSACleanup ();
return 0; }

/* * * * * * send the file content/

If (STRCMP (OK, "OK")==0) {
while(! Feof (fp)) {//when the file is not to the end of the

Size=fread (filebuf, 1, sizeof (filebuf), fp);//every time read 1000 bytes
Send (sockConn filebuf, size, 0).//every time to write the size bytes
}
Cout & lt; <"The file is sent";//show the transmission complete
The fclose (fp); }//close the file



Closesocket (sockSer);
WSACleanup ();
return 0; }

The receiver
 
# include "stdafx. H"
#include
#include
#include
# include "direct h"//_mkdir () function need
# pragma comment (lib, "ws2_32. Lib")
Int main () {
WSADATA wsaData;
If (WSAStartup (MAKEWORD (2, 2), & amp; WsaData)) {
Cout<& lt;" Winsock cannot be initialized!" ;
WSACleanup ();
return 0; }
The SOCKET sockCli;//create a socket sockCli
SockCli=socket (AF_INET SOCK_STREAM, 0).

SOCKADDR_IN addrSer;//as long as the client creates a socket address
AddrSer. Sin_family=AF_INET;
AddrSer. Sin_port=htons (5566);
AddrSer. Sin_addr. S_un. S_addr=inet_addr (" 127.0.0.1 ");

Int res=connect (sockCli, (SOCKADDR *) & amp; AddrSer, sizeof (SOCKADDR));
If (res) {cout<& lt;" The client connect to the server failure "& lt; return -1; }
The else {cout<& lt;" The client connect to the server success "& lt; Recvbuf char sendbuf [256], [256].

Long int filelen;
Char, filedir, [200]="D: \ \ TANG \ ";//specify the received file save directory
Char ok [3]="ok", fsize [50].
Char fileBuffer [1000].//receive file data buffer
Create a file directory */* * * * */
_mkdir (, filedir,);//_mkdir () is used to create the folder
/receiving the file name and file size information * * * * * */
If ((filelen=recv (sockCli, fsize, sizeof (fsize), 0)) & lt;=0) {
Cout<& lt;" Not receive the file name and file size! \ n ";
Closesocket (sockCli); WSACleanup ();
return 0; }
Filelen=atol (fsize);
/* * * create file is ready to receive content * * */
Char filename [256]="e: \ \ cf. JPG";
The FILE * fp.//create a file pointer
If ((fp=fopen (filename, "wb"))==NULL) {//only way to open the file
Cout<& lt;" Cannot open file: "& lt; Closesocket (sockCli);
WSACleanup ();
return 0; }//file open failed exit is
CoutSend (sockCli, ok, sizeof (ok), 0).//to send and receive file data confirmation
/* * * to receive the file data is written to the file * * */
Long int size=0;//the received data length
Do {
Size=recv (sockCli fileBuffer, sizeof (fileBuffer), 0).
Fwrite (fileBuffer, 1, the size, fp);//write to the file, each time to write the size bytes
Filelen -=the size;
} while (the size!=0 & amp; & Filelen> 0);//loop condition is the size!=0 & amp; & Filelen> 0
/* * * * * * end of file transfer completed program/
Cout<& lt;" Receiving the file "& lt; The fclose (fp);

While (1) {
Cout<& lt;" The client said: & gt; ";
Cin> Sendbuf;
If (STRCMP (sendbuf, "bye")==0) {
break; }
Send (sockCli, sendbuf, strlen (sendbuf) + 1, 0).
If (recv (sockCli, recvbuf, 256, 0) & gt; 0)
Cout<& lt;" Server: & gt;" The else {cout<& lt;" Server has closed connection "& lt; break; }
}
Closesocket (sockCli);
WSACleanup ();
return 0;
}

CodePudding user response:

Personal understanding is Ok to make a simple agreement between

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related