Đào Tùng - KTMT K53: "Stdafx.H"
Đào Tùng - KTMT K53: "Stdafx.H"
// // training.cpp : Defines the entry point for the console application. o Tng - KTMT K53 "stdafx.h" <WinSock2.h> <conio.h> <stdio.h> <stdlib.h> <ws2tcpip.h>
void thaoTacCoBanVoiTep() { char path[] = "E:\myfile.dat"; // path to file /********************************************************************** **/ /* fwrite: Write block of data to stream */ /* size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream ) */ /* ptr Pointer to the array of elements to be written. size Size in bytes of each element to be written. count Number of elements, each one with a size of size bytes. stream Pointer to a FILE object that specifies an output stream */ /********************************************************************** **/ FILE * pFile; char buffer1[] = { 'x' , 'y' , 'z' }; pFile = fopen ( path , "wb" ); fwrite (buffer1 , 1 , sizeof(buffer1) , pFile ); // fwrite (buffer1 , 1 , sizeof(buffer1) , pFile ); ghi noi tiep file fclose (pFile); /********************************************************************** **/ /* fread: Read block of data from stream */ /* size_t fread ( void * ptr, size_t size, size_t count, FILE * stream ) */ /* ptr Pointer to a block of memory with a minimum size of (size*count) bytes. size Size in bytes of each element to be read. count Number of elements, each one with a size of size bytes. stream Pointer to a FILE object that specifies an input stream. */
/********************************************************************** **/ /* using feof */ /********************************************************************** **/ long n = 0; pFile = fopen (path,"rb"); if (pFile==NULL) perror ("Error opening file"); else { while (!feof(pFile)) { fgetc (pFile); n++; } fclose (pFile); printf ("Total number of bytes: %d\n", --n); } /********************************************************************** **/
/************************************************************************/ /* Blocking server */ /************************************************************************/ // declaration global for blocking SOCKET clients[64]; SOCKADDR_IN clientAddrs[64]; int clientAddrLen = sizeof(sockaddr); int length = 0; #define MAX_CLIENTS 64
// Khai bao luong nhan du lieu DWORD WINAPI serverReceiveThread(LPVOID lpParam) { int i = (int)lpParam; char buf[1024]; int len; while(1) { len = recv(clients[i], buf, 1024, 0); if (len <= 0) { clients[i] = -1; break; } buf[len] = 0; printf("[%s:%d] %s\n", inet_ntoa(clientAddrs[i].sin_addr), ntohs(clientAddrs[i].sin_port), buf); if (strncmp(buf, "bye", 3) == 0) break; }; printf("Client %d da ngat ket noi!\n", i); return 0; } // Khia bao luong gui du lieu DWORD WINAPI serverSendThread(LPVOID lpParam) { char buf[1024]; while(1) { gets(buf); // nhap xau chao cho server de gui toi clients[i] strcat(buf,"\n"); for (int i = 0; i < MAX_CLIENTS; i++) if (clients[i] != -1) send(clients[i], buf, strlen(buf), 0); } return 0; } void blockingTCPServer() { // declaration blocking WSADATA wsaData; int ret = WSAStartup(MAKEWORD(2,2),&wsaData); if (ret == SOCKET_ERROR) { printf("Error :%d", WSAGetLastError()); goto END; } SOCKET server; SOCKADDR_IN serverAddr; int i; serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(8888); serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); for (i = 0; i < MAX_CLIENTS; i++) clients[i] = -1; // ngat toan bo ket noi tu 64 clients // socket
/************************************************************************/ /* Blocking client */ /************************************************************************/ // declaration blocking client SOCKET client; SOCKADDR_IN serverAddr;