<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=31630&amp;view=findpost&amp;p=212171</guid>
        <pubDate>Wed, 15 Jan 2003 14:10:34 +0000</pubDate>
        <title>Помогите... Работа с файлами в сети</title>
        <link>https://forum.sources.ru/index.php?showtopic=31630&amp;view=findpost&amp;p=212171</link>
        <description><![CDATA[Lyrik: Огромное спасибо &nbsp;;D]]></description>
        <author>Lyrik</author>
        <category>C/C++: Сетевое программирование</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=31630&amp;view=findpost&amp;p=212170</guid>
        <pubDate>Wed, 15 Jan 2003 06:27:12 +0000</pubDate>
        <title>Помогите... Работа с файлами в сети</title>
        <link>https://forum.sources.ru/index.php?showtopic=31630&amp;view=findpost&amp;p=212170</link>
        <description><![CDATA[Lavrik: сервак: <br><div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&#60;br&#62;// server.cpp : Defines the entry point for the application.&#60;br&#62;#include &quot;stdafx.h&quot;&#60;br&#62;#include &#60;ras.h&#62;&#60;br&#62;#include &#60;winsock2.h&#62;&#60;br&#62;#include &#60;windows.h&#62;&#60;br&#62;#include &#60;stdio.h&#62;&#60;br&#62;#include &#60;stdlib.h&#62;&#60;br&#62;#include &#60;conio.h&#62;&#60;br&#62;#include &#60;crtdbg.h&#62;&#60;br&#62;&#60;br&#62;#define &nbsp;PORT &nbsp;28912 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //порт&#60;br&#62;char* ReceiveFile(char* name,int size,SOCKET s); &nbsp;//получение файла&#60;br&#62;int scksend(char* data,SOCKET s); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //посылка сокетом данных &#60;br&#62;int receive(char* buff,int num,SOCKET s); &nbsp; &nbsp; &nbsp; &nbsp; //прием сокетом данных&#60;br&#62;int parser(char* str);&#60;br&#62;void commands(char** list);&#60;br&#62;SOCKET client; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //клиент&#60;br&#62;SOCKET server; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //сервер&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;// &nbsp; &nbsp; &nbsp; MAIN (BODY) PROGRAM&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;int APIENTRY WinMain(HINSTANCE hInstance,&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HINSTANCE hPrevInstance,&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LPSTR &nbsp; &nbsp; lpCmdLine,&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp; &nbsp; &nbsp; nCmdShow)&#60;br&#62;{&#60;br&#62; &nbsp; &nbsp;int i;&#60;br&#62; &nbsp; &nbsp; &nbsp;bool end;&#60;br&#62; &nbsp; &nbsp; &nbsp;char getchar[2];&#60;br&#62; &nbsp; &nbsp; &nbsp;char* argument=(char *)calloc(1024,sizeof(char));&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;//инициализируем процесс библиотеки wsock32.dll&#60;br&#62; &nbsp; &nbsp; &nbsp;WSADATA WsaData;&#60;br&#62; &nbsp; &nbsp; &nbsp;int err = WSAStartup (0x0101, &amp;WsaData);&#60;br&#62; &nbsp; &nbsp; &nbsp;if (err == SOCKET_ERROR)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto EXIT; &#60;br&#62; &nbsp; &nbsp; &nbsp;//теперь объявляем переменную типа SOCKET&#60;br&#62; &nbsp; &nbsp; &nbsp;server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);&#60;br&#62; &nbsp; &nbsp; &nbsp;//задаем параметры для сокета (сервера)&#60;br&#62; &nbsp; &nbsp; &nbsp;if(server==INVALID_SOCKET)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto EXIT;&#60;br&#62; &nbsp; &nbsp; &nbsp;SOCKADDR_IN sin;&#60;br&#62; &nbsp; &nbsp; &nbsp;sin.sin_family = AF_INET;&#60;br&#62; &nbsp; &nbsp; &nbsp;sin.sin_port = htons(PORT);&#60;br&#62; &nbsp; &nbsp; &nbsp;sin.sin_addr.s_addr = INADDR_ANY;&#60;br&#62; &nbsp; &nbsp; &nbsp;//подключаем socket к коммуникационной среде&#60;br&#62; &nbsp; &nbsp; &nbsp;err = bind( server, (LPSOCKADDR)&amp;sin, sizeof(sin) );&#60;br&#62; &nbsp; &nbsp; &nbsp;if(err==-1)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto EXIT;&#60;br&#62; &nbsp; &nbsp; &nbsp;//создаем очереть для ожедания вызовов от клиентов&#60;br&#62; &nbsp; &nbsp; &nbsp;err = listen( server, SOMAXCONN);&#60;br&#62; &nbsp; &nbsp; &nbsp;if(err==-1)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto EXIT;&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;//вечный цикл для многократного обслушивания запросов от клиентов&#60;br&#62; &nbsp; &nbsp; &nbsp;while(true) &nbsp; &nbsp; &nbsp;{&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//принемаем запрос от программ-клиента на установление связи&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SOCKADDR_IN from;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int fromlen=sizeof(from);&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;client = accept(server,(struct sockaddr*)&amp;from, &amp;fromlen);&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(true) &nbsp; &nbsp; &nbsp;{&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;argument[0]= &#39;{text}&#39;;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getchar[0] = &#39;{text}&#39;;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end=false,i=0;&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(!end||i&#62;50) {&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;receive(getchar,1,client);&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getchar[0]=*getchar;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(getchar[0]==&#39;{text}&#39;)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto EXIT;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(getchar[0]!=&#39;\n&#39;) {&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;argument[i]=getchar[0];&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i++;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end=true;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;argument[i]=&#39;{text}&#39;;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(!parser(argument)) //вход в разбор полученной команды &#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto EXIT;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp;}&#60;br&#62;&#60;br&#62;EXIT:&#60;br&#62; &nbsp; &nbsp; &nbsp;free(argument);&#60;br&#62; &nbsp; &nbsp; &nbsp;if (client!=INVALID_SOCKET) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Если клиент еще не слетел, то&#60;br&#62; &nbsp; &nbsp;closesocket(client); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // аккурат закрываем&#60;br&#62; &nbsp; &nbsp; &nbsp;closesocket(server); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // close server socket &#60;br&#62; &nbsp;WSACleanup(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// free winsock&#60;br&#62; &nbsp; &nbsp; &nbsp;return 0;&#60;br&#62;}&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;// &nbsp; &nbsp; &nbsp; Функция отсылки данных клиенту&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;int scksend(char* data,SOCKET s)&#60;br&#62;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// data - строка для отправки&#60;br&#62; &nbsp;int &nbsp;sendchar, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Количество отправленных символов&#60;br&#62; &nbsp; &nbsp; &nbsp; currchar=0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Точка останова отправки&#60;br&#62; &nbsp; &nbsp; &nbsp; ndata; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Длина того, что отправляем&#60;br&#62; &nbsp;ndata=strlen(data);&#60;br&#62; &nbsp;while(currchar&#60;ndata) {&#60;br&#62; &nbsp; &nbsp;sendchar=send(s,(char*)(data + currchar),ndata-currchar,NULL);&#60;br&#62; &nbsp; &nbsp;if(sendchar==SOCKET_ERROR)&#60;br&#62; &nbsp; &nbsp; &nbsp;return 0;&#60;br&#62; &nbsp; &nbsp;currchar+=sendchar;&#60;br&#62; &nbsp;}&#60;br&#62; &nbsp;return 1;&#60;br&#62;};&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;// &nbsp; &nbsp; &nbsp; Функция получения данных от клиента&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;int receive(char* buff,int num,SOCKET s) &nbsp; &nbsp; &nbsp; // массив куда получить и кол-во сколько&#60;br&#62;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// надо получить от сервера&#60;br&#62; &nbsp;int &nbsp;recvchar; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Длина того, что получили&#60;br&#62; &nbsp;recvchar=recv(client,buff,num,NULL);&#60;br&#62; &nbsp;if (recvchar==SOCKET_ERROR)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;return 0;//goto EXIT;// SERROR();&#60;br&#62; &nbsp;buff[recvchar]=&#39;{text}&#39;;&#60;br&#62; &nbsp;return recvchar;&#60;br&#62;};&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;// &nbsp; &nbsp; &nbsp; Функция разбиения строки на команды&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;int parser(char* str)&#60;br&#62;{&#60;br&#62; &nbsp; &nbsp; &nbsp;int j,st=0,count=0;bool sp=true;;&#60;br&#62; &nbsp; &nbsp; &nbsp;char **s=(char **)calloc(3,sizeof(char*));&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;for(j=0;j&#60;3;j++)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s[j]=(char *)calloc(1024,sizeof(char));&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;for(int i=0;i&#60;strlen(str);i++){&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (str[i] == &#39;$&#39;){&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!sp){&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count=0;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;st++;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sp = true;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else{&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sp = false;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s[st][count]=str[i];&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count++;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp;commands(s); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //выполнение соответсвующей команды&#60;br&#62; &nbsp; &nbsp; &nbsp;for(j=0;j&#60;Number;j++){&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s[j]=&#39;{text}&#39;;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete[]s[j];&#60;br&#62; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp;return 0;&#60;br&#62;}&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;// &nbsp; &nbsp; &nbsp; Функция выбора команды и выполнение ее&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;void commands(char** list)&#60;br&#62;{&#60;br&#62; &nbsp; &nbsp; &nbsp;if(strcmp(list[0],&quot;FILE&quot;)==0)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scksend(ReceiveFile(list[1],atoi(list[2]),client),client);&#60;br&#62;&#60;br&#62;}&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;// &nbsp; &nbsp; &nbsp; Подпрограмммы получения файла&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;char* ReceiveFile(char* name,int size,SOCKET s)&#60;br&#62;{&#60;br&#62; &nbsp; &nbsp; &nbsp;int nTotalBytes=0,nDifferentBytes=0; unsigned long nBytesWrite;&#60;br&#62; &nbsp; &nbsp; &nbsp;bool end=false; &nbsp; &nbsp; &nbsp;&#60;br&#62; &nbsp; &nbsp; &nbsp;char* buffer=(char*)calloc(1025,sizeof(char));&#60;br&#62;&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;char path[256];&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;sprintf(path,&quot;c:\\%s&quot;,name);&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;HANDLE hFile=CreateFile(path,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);&#60;br&#62; &nbsp; &nbsp; &nbsp;&#60;br&#62; &nbsp; &nbsp; &nbsp;if(hFile==INVALID_HANDLE_VALUE)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &quot;Can&#39;t create file&quot;;&#60;br&#62; &nbsp; &nbsp; &nbsp;while( size&#62;nTotalBytes) {&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(size-nTotalBytes&#62;1024)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nDifferentBytes=1024;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if(size-nTotalBytes&#60;1024 &amp;&amp; size-nTotalBytes&#62;0)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nDifferentBytes=size-nTotalBytes;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int &nbsp;recvchar;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;recvchar=recv(client,buffer,nDifferentBytes,NULL);&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;buffer[recvchar]=&#39;{text}&#39;;&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(!WriteFile(hFile,buffer,recvchar,&amp;nBytesWrite,NULL))&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &quot;Can&#39;t write to file&quot;;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nTotalBytes+=nBytesWrite;&#60;br&#62; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp;CloseHandle(hFile);&#60;br&#62; &nbsp; &nbsp; &nbsp;free(buffer);&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;if(nTotalBytes==size)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &quot;File normal received&quot;;&#60;br&#62; &nbsp; &nbsp; &nbsp;else if(nTotalBytes&#62;size)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &quot;In file write garbage&quot;;&#60;br&#62; &nbsp; &nbsp; &nbsp;return &quot;File not received&quot;;&#60;br&#62;}&#60;br&#62;</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script><br><br>клиент:<br><div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&#60;br&#62;// clientdos.cpp : Defines the entry point for the console application.&#60;br&#62;#include &quot;stdafx.h&quot;&#60;br&#62;#include &#60;winsock2.h&#62;&#60;br&#62;#include &#60;windows.h&#62;&#60;br&#62;#include &#60;stdio.h&#62;&#60;br&#62;#include &#60;stdio.h&#62;&#60;br&#62;#include &#60;iostream.h&#62;&#60;br&#62;#include &#60;conio.h&#62;&#60;br&#62;&#60;br&#62;void SEND(char *data,int len,SOCKET s);&#60;br&#62;void SendFile(SOCKET s);&#60;br&#62;void SendMail(SOCKET s);&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;// &nbsp; &nbsp; &nbsp; MAIN (BODY) PROGRAM&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;int main(int argc, char* argv[])&#60;br&#62;{&#60;br&#62; &nbsp; &nbsp; &nbsp;char* address=new char[1024];&#60;br&#62; &nbsp; &nbsp; &nbsp;char* cmd=new char[2];&#60;br&#62; &nbsp; &nbsp; &nbsp;printf(&quot;Please enter adress as 127.0.0.1\n&quot;);&#60;br&#62; &nbsp; &nbsp; &nbsp;gets(address);&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;//инициализируем процесс библиотеки wsock32.dll&#60;br&#62; &nbsp;WSADATA WsaData;&#60;br&#62; &nbsp;WSAStartup (257, &amp;WsaData);&#60;br&#62;&#60;br&#62; &nbsp;//теперь объявляем переменную типа SOCKET&#60;br&#62; &nbsp;SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);&#60;br&#62; &nbsp; &nbsp; &nbsp;SOCKADDR_IN sin;&#60;br&#62; &nbsp;ZeroMemory(&amp;sin, sizeof(sin)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Зачистка&#60;br&#62; &nbsp;sin.sin_family = AF_INET; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Тип соединения&#60;br&#62; &nbsp;sin.sin_port = htons(28912); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Порт&#60;br&#62; &nbsp;sin.sin_addr.s_addr =inet_addr(address); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // IP адрес &#60;br&#62; &nbsp; &nbsp; &nbsp;&#60;br&#62; &nbsp;if(connect(s, (PSOCKADDR)&amp;sin, sizeof(SOCKADDR))==-1){&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;fail connect to server\n&quot;);&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;goto EXIT;&#60;br&#62; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp;printf(&quot;connect to server ok\n&quot;);&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;&#60;br&#62;BEGIN:&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;printf(&quot;1 - for send file,0 - for EXIT\n&quot;);&#60;br&#62; &nbsp; &nbsp; &nbsp;gets(cmd);&#60;br&#62; &nbsp; &nbsp; &nbsp;switch(atoi(cmd)){&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 0:&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto EXIT;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 1:&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendFile(s);&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&#60;br&#62; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp;char str[256];&#60;br&#62; &nbsp; &nbsp; &nbsp;int &nbsp;a; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#60;br&#62; &nbsp; &nbsp;a=recv(s,str,sizeof(str),NULL);&#60;br&#62; &nbsp; &nbsp; &nbsp;str[a]=&#39;{text}&#39;;&#60;br&#62; &nbsp; &nbsp; &nbsp;printf(&quot;\n\%s\n&quot;,str);&#60;br&#62; &nbsp; &nbsp; &nbsp;goto BEGIN;&#60;br&#62;&#60;br&#62;EXIT:&#60;br&#62; &nbsp; &nbsp; &nbsp;free(address);free(cmd);&#60;br&#62; &nbsp; &nbsp; &nbsp;if (s!=INVALID_SOCKET) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Если клиент еще не слетел, то&#60;br&#62; &nbsp; &nbsp;closesocket(s); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// аккурат закрываем&#60;br&#62; &nbsp;WSACleanup(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// free winsock&#60;br&#62; &nbsp; &nbsp; &nbsp;return 0;&#60;br&#62;}&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;// &nbsp; &nbsp; &nbsp; Функция отсылки данных клиенту&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;void SEND(char *data,int len,SOCKET s)&#60;br&#62;{ &nbsp; &nbsp; &nbsp;&#60;br&#62; &nbsp; &nbsp; &nbsp;int &nbsp;a, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Количество отправленных символов&#60;br&#62; &nbsp; &nbsp; &nbsp; b=0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Точка останова отправки&#60;br&#62; &nbsp; &nbsp; &nbsp; ndata; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Длина того, что отправляем&#60;br&#62; &nbsp;ndata=len;&#60;br&#62; &nbsp;while(b&#60;ndata) {&#60;br&#62; &nbsp; &nbsp;a=send(s,(char*)(data + b),ndata-b,NULL);&#60;br&#62; &nbsp; &nbsp;b+=a;&#60;br&#62; &nbsp;}&#60;br&#62;}&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;// &nbsp; &nbsp; &nbsp; Подпрограмммы посылки файла&#60;br&#62;//---------------------------------------------------------------------------&#60;br&#62;void SendFile(SOCKET s)&#60;br&#62;{&#60;br&#62; &nbsp; &nbsp; &nbsp;char* fullname=new char[1024];&#60;br&#62; &nbsp; &nbsp; &nbsp;char* name=new char[1024];&#60;br&#62; &nbsp; &nbsp; &nbsp;name[0]=&#39;{text}&#39;;&#60;br&#62; &nbsp; &nbsp; &nbsp;&#60;br&#62; &nbsp; &nbsp; &nbsp;printf(&quot;ENTER FULL PATH\n&quot;);&#60;br&#62; &nbsp; &nbsp; &nbsp;gets(fullname);&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;int Total=0, bResult=1;&#60;br&#62; &nbsp;unsigned long int nBytesRead=1;&#60;br&#62; &nbsp;char * inBuffer=new char[1025];&#60;br&#62;&#60;br&#62; &nbsp;HANDLE hFile=CreateFile(fullname, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//name&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GENERIC_READ, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//property&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NULL,&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NULL,&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OPEN_EXISTING, &nbsp; &nbsp; &nbsp; &nbsp; //how open?&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_ATTRIBUTE_NORMAL, //attr&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NULL); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //template&#60;br&#62; &nbsp; &nbsp; &nbsp;if(hFile==INVALID_HANDLE_VALUE){&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;Error open file&quot;);&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit(0);&#60;br&#62; &nbsp; &nbsp; &nbsp;}&#60;br&#62;&#60;br&#62; &nbsp;int Size=GetFileSize(hFile,NULL);&#60;br&#62; &nbsp; &nbsp; &nbsp;char* size=new char[1024];&#60;br&#62; &nbsp; &nbsp; &nbsp;itoa(Size,size,10);&#60;br&#62;&#60;br&#62; &nbsp; &nbsp; &nbsp;strrev(fullname);&#60;br&#62; &nbsp; &nbsp; &nbsp;for(int i=0;fullname[i]!=&#39;\&#39;;i++)&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name[i]=fullname[i];&#60;br&#62; &nbsp; &nbsp; &nbsp;name[i]=&#39;{text}&#39;;&#60;br&#62; &nbsp; &nbsp; &nbsp;strrev(name);&#60;br&#62;&#60;br&#62; &nbsp;send(s,&quot;FILE$&quot;,strlen(&quot;FILE$&quot;),NULL);&#60;br&#62; &nbsp; &nbsp; &nbsp;send(s,name,strlen(name),NULL);&#60;br&#62; &nbsp; &nbsp; &nbsp;send(s,&quot;$&quot;,1,NULL);&#60;br&#62; &nbsp; &nbsp; &nbsp;send(s,size,strlen(size),NULL);&#60;br&#62; &nbsp; &nbsp; &nbsp;send(s,&quot;\n&quot;,1,NULL);&#60;br&#62;&#60;br&#62; &nbsp;while(bResult &amp;&amp; nBytesRead != 0){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//While not eof.......&#60;br&#62; &nbsp; bResult = ReadFile(hFile,inBuffer,1024,&amp;nBytesRead, NULL);&#60;br&#62;&#60;br&#62; &nbsp; if(bResult &amp;&amp; nBytesRead!=0 ){&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Total+=nBytesRead; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Send buf and his size&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SEND(inBuffer,nBytesRead,s);&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;.&quot;);&#60;br&#62; &nbsp; }&#60;br&#62; &nbsp; &nbsp; &nbsp;}&#60;br&#62; &nbsp; &nbsp; &nbsp;CloseHandle(hFile); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#60;br&#62;}&#60;br&#62;</div></ol></div></div></div></div><br><br>а теперя принцип работы (это я вырезал кусок с проги когда-то писал чтото типа RAdmin) - сервак получает команду FILE$ИМЯ_ФАЙЛА$РАЗМЕР_ФАЙЛА\n, концом каждой команды является символ \n. Затем команда разбирается на куски отделяемые $.<br>Затем все это передается в соответсвующую подрограмму с соответсвующими аргументами. В принципе можно самому задавать свои прототипы команд - главное шобы на серваке был их разбор и выполнение.<br><br>з.ы. вышеприведенный код можно запихать в класс, почистить/улучшить - все на усмотрение. Это ОЧЕНЬ простой пример - хдесь нет ни проверок на ошибок - вообще ничего практически нету - так что это только сам принцип (по крайней мере как это понимаю я). Так что ясен пень этот код никакой критике не выдержит ;D]]></description>
        <author>Lavrik</author>
        <category>C/C++: Сетевое программирование</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=31630&amp;view=findpost&amp;p=212169</guid>
        <pubDate>Tue, 14 Jan 2003 19:24:15 +0000</pubDate>
        <title>Помогите... Работа с файлами в сети</title>
        <link>https://forum.sources.ru/index.php?showtopic=31630&amp;view=findpost&amp;p=212169</link>
        <description><![CDATA[Lyrik: Кто-нибудь знает как отослать\принять файл по сети (TCP/IP) с использованием WinSock.<br>Желательно поподробнее<br> :-[]]></description>
        <author>Lyrik</author>
        <category>C/C++: Сетевое программирование</category>
      </item>
	
      </channel>
      </rss>
	