我是靠谱客的博主 虚拟金鱼,最近开发中收集的这篇文章主要介绍windows基于TCP/IP的简单文件/图片传输,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

详细函数资料参考msdn

// TransPic_Server.cpp : 定义控制台应用程序的入口点。
/*
	Server.cpp
*/
//

#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

#define BUF 256
#pragma comment(lib, "Ws2_32.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	WSADATA wsaData = { 0 };
	int iResult = 0;
	// Initialize Winsock
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != 0) {
		wprintf(L"WSAStartup failed: %dn", iResult);
		return 1;
	}

	struct sockaddr_in servaddr;
	int bResult = 0;
	SOCKET sock = INVALID_SOCKET;
	char buf[BUF];

	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
	if (sock == INVALID_SOCKET) {
		wprintf(L"socket failed with error: %ldn", GetLastError());
		WSACleanup();
		return 1;
	}

	servaddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(8888);

	bResult = bind(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr_in));
	if (bResult == INVALID_SOCKET) {
		wprintf(L"bind failed with error: %ldn", GetLastError());
		closesocket(sock);
		WSACleanup();
		return 1;
	}

	if (listen(sock, 256) == SOCKET_ERROR)
	{
		wprintf(L"listen failed with error: %ldn", GetLastError());
		closesocket(sock);
		WSACleanup();
		return 1;
	}

	SOCKET acceptsock = INVALID_SOCKET;

	printf("Accepting ...n");
	if ((acceptsock = accept(sock, NULL, NULL)) == INVALID_SOCKET)
	{
		wprintf(L"accept failed with error: %ldn", GetLastError());
		closesocket(sock);
		WSACleanup();
		return 1;
	}
	else
		printf("Client connected.n");

	FILE *fp = NULL;
	// 不能以二进制打开txt耶
	if ((fp = fopen("popkart.jpg", "rb")) == NULL)
	{
		wprintf(L"fopen failed with error: %ldn", GetLastError());
		closesocket(sock);
		WSACleanup();
		return 1;
	}
	size_t size = 0;
	while (!feof(fp))
	{
		size = fread(buf, sizeof(char), sizeof(buf), fp);
		if (send(acceptsock, buf, size, 0) == SOCKET_ERROR)
		{
			wprintf(L"send failed with error: %ldn", GetLastError());
			closesocket(sock);
			closesocket(acceptsock);
			WSACleanup();
			return 1;
		}
	}
	printf("send picture finish.n");
	
	// 将缓冲区的内容全发送出去,调用closesock之前不可少
	shutdown(acceptsock, SD_SEND);
	closesocket(acceptsock);
	closesocket(sock);
	WSACleanup();

	system("pause");
	return 0;
}


// TransPic_Client.cpp : 定义控制台应用程序的入口点。
/*
	Client.cpp
*/
//

#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

#define BUF 256
#pragma comment(lib, "Ws2_32.lib")

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

	//----------------------
	// Initialize Winsock
	WSADATA wsaData;
	int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != NO_ERROR) {
		wprintf(L"WSAStartup function failed with error: %dn", iResult);
		return 1;
	}
	//----------------------
	// Create a SOCKET for connecting to server
	SOCKET ConnectSocket;
	ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (ConnectSocket == INVALID_SOCKET) {
		wprintf(L"socket function failed with error: %ldn", WSAGetLastError());
		WSACleanup();
		return 1;
	}
	//----------------------
	// The sockaddr_in structure specifies the address family,
	// IP address, and port of the server to be connected to.
	sockaddr_in clientService;
	clientService.sin_family = AF_INET;
	clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
	clientService.sin_port = htons(8888);

	//----------------------
	// Connect to server.
	iResult = connect(ConnectSocket, (SOCKADDR *)& clientService, sizeof(clientService));
	if (iResult == SOCKET_ERROR) {
		wprintf(L"connect function failed with error: %ldn", WSAGetLastError());
		iResult = closesocket(ConnectSocket);
		if (iResult == SOCKET_ERROR)
			wprintf(L"closesocket function failed with error: %ldn", WSAGetLastError());
		WSACleanup();
		return 1;
	}
	wprintf(L"Connected to server.n");

	//------------------------
	// Start to recv picture data
	FILE *fp = NULL;
	if ((fp = fopen("popkart.jpg", "wb+")) == NULL)
	{
		closesocket(ConnectSocket);
		wprintf(L"fopen function failed with error: %ldn", WSAGetLastError());
		WSACleanup();
		return 1;
	}

	size_t size = 0;
	char buf[BUF];
	do
	{
		size = recv(ConnectSocket, buf, sizeof(buf), MSG_WAITALL);
		// 对端调用了closesock,则返回SOCKET_ERROR
		if (size == SOCKET_ERROR)
		{
			break;
		}
		fwrite(buf, size, 1, fp);
		// 这里,循环条件是读取的字节数是否等于BUF,可能出现文件大小刚好是BUF的整数倍,故shutdown来辅助判断
	} while (size == sizeof(buf));

	fclose(fp);

	printf("read finish.n");
	// End recv
	//------------------------
	

	iResult = closesocket(ConnectSocket);
	if (iResult == SOCKET_ERROR) {
		wprintf(L"closesocket function failed with error: %ldn", WSAGetLastError());
		WSACleanup();
		return 1;
	}

	WSACleanup();
	system("pause");
	return 0;
}



最后

以上就是虚拟金鱼为你收集整理的windows基于TCP/IP的简单文件/图片传输的全部内容,希望文章能够帮你解决windows基于TCP/IP的简单文件/图片传输所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部