Red de conocimiento informático - Computadora portátil - Programación de sockets en lenguaje C para realizar la transmisión de datos en red

Programación de sockets en lenguaje C para realizar la transmisión de datos en red

Solo como referencia

// serverTCP.cpp: define el punto de entrada de la aplicación de consola.

//

#include "stdafx.h"

usando el espacio de nombres std;

long long cnt = 0;

void transFile(SOCKET s)

{

printf("Nuevo subservicio d...", cnt);

enviar(s, "bienvenido al SERVIDOR DE ARCHIVOS TCP!", strlen("¡bienvenido al SERVIDOR DE ARCHIVOS TCP!") 1, 0);

char Buf[BUFSIZE]

FILE *fp;

//printf("El cliente ha sido abierto\nIngrese la dirección del archivo:\n");

char FilePath[128]={"0"} ;

ltoa(cnt, FilePath, 10);

if((fp=fopen(FilePath, "wb"))==NULL)

{

printf("Archivo no abierto\n");

return

}

else

{

send(s, "Iniciar transmisión", strlen("Iniciar transmisión") 1, 0

//Obtener el tamaño del archivo

char Tamaño [20] ;

long int FileSize=0

recv(s, Tamaño, 21, 0

FileSize=atol(Tamaño);

printf("Obtener tamaño de archivo: d\n", FileSize

//Iniciar transmisión

char Block[BUFSIZE];

long); int x=0;

while (1)

{

x = BUFSIZE;

if(x lt; FileSize)

{

recv(s, Bloque, BUFSIZE 1, 0);

fwrite(Bloque, 1, BUFSIZE, fp); > }

else

{

recv(s, Bloque, Tamaño de archivo BUFSIZE-x 1, 0;

printf("); Recepción de archivo completada\n");

fwrite(Block, 1, FileSize BUFSIZE-x, fp);

fclose(fp);

break;

}

}

}

fclose(fp);

closesocket(s) ;

}

int _tmain(int argc, _TCHAR* argv[])

{

WORD myVersionRequest;

WSADATA wsaData;

myVersionRequest=MAKEWORD(1,1);

int er

r;

err = WSAStartup(myVersionRequest, & wsaData);

if (!err)

{

printf(" Servidor inicio...\n");

}

else

{

printf("¡Error al iniciar el servidor! ");

exit(0);

}

SOCKET serSocket = socket(AF_INET, SOCK_STREAM, 0 // Creó una interfaz de socket reconocida

SOCKADDR_IN addr;

addr.sin_family=AF_INET;

addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY); //dirección IP

addr.sin_port=htons(PORTBASE); //Puerto de enlace

bind(serSocket, (SOCKADDR*)amp; addr, sizeof(SOCKADDR) //Enlace completado

escucha (serSocket, ACESIZE); //El segundo parámetro representa el número máximo de conexiones que se pueden recibir

SOCKADDR_IN clientAddr;

int len ​​​​= sizeof (SOCKADDR

while(1)

{

cnt;

SOCKET serConn

serConn = aceptar(serSocket, ( SOCKADDR*)amp; clientAddr, amp; len); //Si esto no es aceptar sino conexión. . Continuará monitoreando

if(_beginthread((void (*)(void *))transFile, ACESIZE, (void *)serConn) lt; 0) return 0;

}

return 0;

}

// clientTCP.cpp: define el punto de entrada para la aplicación de consola.

//

#include "stdafx.h"

usando el espacio de nombres std;

int _tmain(int argc, _TCHAR* argv [])

{

WORD myVersionRequest;

WSADATA wsaData

myVersionRequest=MAKEWORD(1, 1); >

int err;

err=WSAStartup(myVersionRequest, & wsaData);

if (!err)

{

printf("Socket abierto\n");

}

else

{

// Enlace adicional Definir el socket

printf("¡El socket no está abierto!");

return 0;

}

SOCKET cliSocket =socket(AF_INET, SOCK_STREAM, 0);

dirección SOCKADDR_IN

char ip_addr[16]={"127.0.0.1"}

dirección sin_addr.S_un.S_addr. =inet_addr(ip_addr);

addr.sin_family=AF_INET;

addr.sin_port=htons(PORT);

char ACK[ 64];

connect(cliSocket, (SOCKADDR*)amp; addr, sizeof(SOCKADDR)); //Comenzar a conectar

recv(cliSocket, ACK, sizeof(ACK),

printf("s\n", ACK);

ARCHIVO *fp;

int FileSize=0

char Block[BUFSIZE; ]={"0"};

char FilePath[128]={"0"};

int i=0;

hacer

{

printf("Ingrese la dirección del archivo:\n");

gets(FilePath); /p>

if((fp=fopen(FilePath, "rb"))==NULL)

{

i = 1;

printf("Error al abrir el archivo\n");

}

} while(i

fseek(fp, 0L, SEEK_END

FileSize=ftell(fp);

printf("Tamaño del archivo a transferir: d\n", FileSize);

printf("Esperando el servidor); acepta...\n");

recv(cliSocket, Block, sizeof(Block), 0);

printf("s\n", Block );

if(strcmp(Bl

ock, "Comenzar a transmitir")==0)

{

char Size[20];

ltoa(FileSize, Size, 10);

p>

send(cliSocket, Tamaño, sizeof(Tamaño), 0

fseek(fp, 0L, SEEK_SET

long int y); =0;

p>

char trans[BUFSIZE];

while(!feof(fp))

{

fread(trans, 1, BUFSIZE, fp );

y=y BUFSIZE;

if(ylt;FileSize)

{

enviar(cliSocket, trans, BUFSIZE 1 , 0);

}

else

{

enviar(cliSocket, trans) , Tamaño de archivo BUFSIZE-y 1, 0);

closesocket(cliSocket

WSACleanup()

}

}

}

printf("Archivo enviado completado\n");

fclose(fp);

closesocket(cliSocket); /p>

WSACleanup();

sistema("pausa");

devuelve 0;