Kaynağa Gözat

second week

jiangyinying
JiangYinying 1 yıl önce
işleme
addb109587
83 değiştirilmiş dosya ile 796 ekleme ve 0 silme
  1. BIN
     
  2. BIN
     
  3. BIN
     
  4. BIN
     
  5. BIN
     
  6. BIN
     
  7. BIN
     
  8. BIN
     
  9. BIN
     
  10. BIN
     
  11. BIN
     
  12. BIN
     
  13. BIN
     
  14. BIN
     
  15. BIN
     
  16. BIN
     
  17. BIN
     
  18. BIN
     
  19. BIN
     
  20. BIN
     
  21. BIN
     
  22. +22
    -0
      第二周/client/Debug/client.log
  23. BIN
     
  24. BIN
     
  25. BIN
     
  26. BIN
     
  27. +2
    -0
      第二周/client/Debug/client.tlog/client.lastbuildstate
  28. BIN
     
  29. BIN
     
  30. BIN
     
  31. BIN
     
  32. BIN
     
  33. BIN
     
  34. BIN
     
  35. BIN
     
  36. BIN
     
  37. BIN
     
  38. +33
    -0
      第二周/client/client.c
  39. BIN
     
  40. +137
    -0
      第二周/client/client.vcxproj
  41. +36
    -0
      第二周/client/client.vcxproj.filters
  42. +4
    -0
      第二周/client/client.vcxproj.user
  43. +5
    -0
      第二周/readme.txt
  44. +41
    -0
      第二周/server.sln
  45. BIN
     
  46. BIN
     
  47. BIN
     
  48. BIN
     
  49. +1
    -0
      第二周/server/Debug/server.log
  50. BIN
     
  51. BIN
     
  52. BIN
     
  53. BIN
     
  54. BIN
     
  55. BIN
     
  56. BIN
     
  57. +2
    -0
      第二周/server/Debug/server.tlog/server.lastbuildstate
  58. BIN
     
  59. BIN
     
  60. BIN
     
  61. BIN
     
  62. BIN
     
  63. BIN
     
  64. +47
    -0
      第二周/server/server.c
  65. BIN
     
  66. +137
    -0
      第二周/server/server.vcxproj
  67. +36
    -0
      第二周/server/server.vcxproj.filters
  68. +4
    -0
      第二周/server/server.vcxproj.user
  69. BIN
     
  70. BIN
     
  71. +20
    -0
      第二周/server/x64/Debug/server.log
  72. BIN
     
  73. BIN
     
  74. BIN
     
  75. BIN
     
  76. +2
    -0
      第二周/server/x64/Debug/server.tlog/server.lastbuildstate
  77. +0
    -0
     
  78. BIN
     
  79. BIN
     
  80. +121
    -0
      第二周/tcpSocket/fileOperation.c
  81. +41
    -0
      第二周/tcpSocket/fileOperation.h
  82. +84
    -0
      第二周/tcpSocket/tcpSocket.c
  83. +21
    -0
      第二周/tcpSocket/tcpSocket.h





















+ 22
- 0
第二周/client/Debug/client.log Dosyayı Görüntüle

@@ -0,0 +1,22 @@
 fileOperation.c
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(11): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\stdio.h(208): note: 参见“fopen”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(47): warning C4996: '_splitpath': This function or variable may be unsafe. Consider using _splitpath_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\stdlib.h(1112): note: 参见“_splitpath”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(48): warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\string.h(90): note: 参见“strcat”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(49): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\string.h(133): note: 参见“strcpy”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(72): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\string.h(133): note: 参见“strcpy”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(110): warning C4047: “函数”:“const char *”与“char (*)[256]”的间接级别不同
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(110): warning C4024: “fopen”: 形参和实参 1 的类型不同
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(110): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\stdio.h(208): note: 参见“fopen”的声明
tcpSocket.c
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\tcpsocket.c(74): warning C4133: “函数”: 从“sockaddr_in *”到“const sockaddr *”的类型不兼容
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\tcpsocket.c(71): warning C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
d:\windows kits\10\include\10.0.17763.0\um\winsock2.h(1831): note: 参见“inet_addr”的声明
client.c
正在生成代码...
client.vcxproj -> F:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\Debug\client.exe





+ 2
- 0
第二周/client/Debug/client.tlog/client.lastbuildstate Dosyayı Görüntüle

@@ -0,0 +1,2 @@
#TargetFrameworkVersion=v4.0:PlatformToolSet=v141:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=10.0.17763.0
Debug|Win32|F:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\|











+ 33
- 0
第二周/client/client.c Dosyayı Görüntüle

@@ -0,0 +1,33 @@
#include"../tcpSocket/tcpSocket.h"
#include"../tcpSocket/fileOperation.h"
int main()
{
init_Socket();

SOCKET fd = create_clientSocket("127.0.0.1"); //传入本地ip地址
printf("连接成功\n");
//struct MsgHeader msg = { .msgID = MSG_READY_READ,.fileInfo.fileName = "toby.jpg" };
recvFile(fd);

//ipconfig,可以填默认网关192.168.0.1,或者127.0.0.1实现本地回还
//char recvBuf[1024] = ""; //定义缓冲区2^10
//int ret = recv(fd, recvBuf, 1024, 0);
//if (ret == 0)
//{
// printf("服务器正常下线...\n"); //返回值ret=0客户端正常下线
//}
//else if (ret < 0)
//{
// err("recv");
//}

//puts(recvBuf); //在客户端上显示服务器发送的消息
//char sendBuf[1024] = "客户端给服务器回发的消息";
//send(fd, sendBuf, strlen(sendBuf), 0);

closesocket(fd); //关socket需要传入socket参数
close_Socket();
getchar();

return 0;
}


+ 137
- 0
第二周/client/client.vcxproj Dosyayı Görüntüle

@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}</ProjectGuid>
<RootNamespace>client</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>false</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\tcpSocket\fileOperation.c" />
<ClCompile Include="..\tcpSocket\tcpSocket.c" />
<ClCompile Include="client.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\tcpSocket\fileOperation.h" />
<ClInclude Include="..\tcpSocket\tcpSocket.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

+ 36
- 0
第二周/client/client.vcxproj.filters Dosyayı Görüntüle

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\tcpSocket\tcpSocket.c">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="client.c">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="..\tcpSocket\fileOperation.c">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\tcpSocket\tcpSocket.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="..\tcpSocket\fileOperation.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

+ 4
- 0
第二周/client/client.vcxproj.user Dosyayı Görüntüle

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

+ 5
- 0
第二周/readme.txt Dosyayı Görüntüle

@@ -0,0 +1,5 @@
完成时间:20230726
代码开发环境:Visual Studio
开发语言:C
实现功能:通信模块,客户端和服务器端socket通信实现文件传输
具体实现:实现服务器向客户端传输文件,将服务器和客户端分开编写,服务器的ip设置为127.0.0.1实现本地回环,即将处于server文件夹中的文件传输到client文件夹中,编程完成后将项目转换为可执行文件(server.exe和client.exe),分别保存到test文件夹中的server文件夹和client文件夹中备份。

+ 41
- 0
第二周/server.sln Dosyayı Görüntüle

@@ -0,0 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.1927
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "server", "server\server.vcxproj", "{0074D7E8-BE6E-40A4-8608-2D65566C9239}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "client", "client\client.vcxproj", "{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0074D7E8-BE6E-40A4-8608-2D65566C9239}.Debug|x64.ActiveCfg = Debug|x64
{0074D7E8-BE6E-40A4-8608-2D65566C9239}.Debug|x64.Build.0 = Debug|x64
{0074D7E8-BE6E-40A4-8608-2D65566C9239}.Debug|x86.ActiveCfg = Debug|Win32
{0074D7E8-BE6E-40A4-8608-2D65566C9239}.Debug|x86.Build.0 = Debug|Win32
{0074D7E8-BE6E-40A4-8608-2D65566C9239}.Release|x64.ActiveCfg = Release|x64
{0074D7E8-BE6E-40A4-8608-2D65566C9239}.Release|x64.Build.0 = Release|x64
{0074D7E8-BE6E-40A4-8608-2D65566C9239}.Release|x86.ActiveCfg = Release|Win32
{0074D7E8-BE6E-40A4-8608-2D65566C9239}.Release|x86.Build.0 = Release|Win32
{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}.Debug|x64.ActiveCfg = Debug|x64
{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}.Debug|x64.Build.0 = Debug|x64
{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}.Debug|x86.ActiveCfg = Debug|Win32
{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}.Debug|x86.Build.0 = Debug|Win32
{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}.Release|x64.ActiveCfg = Release|x64
{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}.Release|x64.Build.0 = Release|x64
{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}.Release|x86.ActiveCfg = Release|Win32
{B99B1E8E-6AD1-4F95-A98C-0BA8AC3F820E}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AA084055-FEA9-44EA-A96C-0B989F394FB5}
EndGlobalSection
EndGlobal





+ 1
- 0
第二周/server/Debug/server.log Dosyayı Görüntüle

@@ -0,0 +1 @@
 server.vcxproj -> F:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\Debug\server.exe








+ 2
- 0
第二周/server/Debug/server.tlog/server.lastbuildstate Dosyayı Görüntüle

@@ -0,0 +1,2 @@
#TargetFrameworkVersion=v4.0:PlatformToolSet=v141:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=10.0.17763.0
Debug|Win32|F:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\|







+ 47
- 0
第二周/server/server.c Dosyayı Görüntüle

@@ -0,0 +1,47 @@
#include"../tcpSocket/tcpSocket.h"
#include"../tcpSocket/fileOperation.h"
//ctrl+k+c

int main()
{
init_Socket();

SOCKET serfd = create_serverSocket(); //创建服务器socket
printf("server has created successfully,waiting for client connect..\n");
//等待客户端连接
SOCKET clifd = accept(serfd,NULL,NULL); //创建用于连接的客户端socket
if (clifd == INVALID_SOCKET) //失败情况
{
err("accept");
}
printf("客户端发起与服务器的连接,成功...\n");
//可以与客户端进行通信
struct MsgHeader msg = { .msgID = MSG_SEND,.fileInfo.fileName = "./AutoHotkey_setup.exe" };
sendFile(clifd, (char*)&msg); //发送的文件名


//if (SOCKET_ERROR == send(clifd, "服务器向客户端发来的消息", 24, 0)) // 字符个数,中文1个汉字=2个字符
//{
// err("dingyaya send");
//}

//char recvBuf[1024] = ""; //定义缓冲区2^10
//int ret = recv(clifd, recvBuf, 1024, 0); //接收客户端发来的
//if (ret == 0)
//{
// printf("客户端正常下线...\n"); //返回值ret=0客户端正常下线
//}
//else if (ret < 0)
//{
// err("recv");
//}
//puts(recvBuf); //在客户端上显示服务器发送的消息

closesocket(clifd); //系统函数,关闭客户端
closesocket(serfd);

close_Socket();
getchar(); //防止闪退

return 0;
}


+ 137
- 0
第二周/server/server.vcxproj Dosyayı Görüntüle

@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{0074D7E8-BE6E-40A4-8608-2D65566C9239}</ProjectGuid>
<RootNamespace>server</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>false</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\tcpSocket\fileOperation.c" />
<ClCompile Include="..\tcpSocket\tcpSocket.c" />
<ClCompile Include="server.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\tcpSocket\fileOperation.h" />
<ClInclude Include="..\tcpSocket\tcpSocket.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

+ 36
- 0
第二周/server/server.vcxproj.filters Dosyayı Görüntüle

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="server.c">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="..\tcpSocket\tcpSocket.c">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="..\tcpSocket\fileOperation.c">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\tcpSocket\tcpSocket.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="..\tcpSocket\fileOperation.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

+ 4
- 0
第二周/server/server.vcxproj.user Dosyayı Görüntüle

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>



+ 20
- 0
第二周/server/x64/Debug/server.log Dosyayı Görüntüle

@@ -0,0 +1,20 @@
 fileOperation.c
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(11): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\stdio.h(208): note: 参见“fopen”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(47): error C4996: '_splitpath': This function or variable may be unsafe. Consider using _splitpath_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\stdlib.h(1112): note: 参见“_splitpath”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(48): error C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\string.h(90): note: 参见“strcat”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(49): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\string.h(133): note: 参见“strcpy”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(72): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\string.h(133): note: 参见“strcpy”的声明
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(110): warning C4047: “函数”:“const char *”与“char (*)[256]”的间接级别不同
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(110): warning C4024: “fopen”: 形参和实参 1 的类型不同
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\fileoperation.c(110): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
d:\windows kits\10\include\10.0.17763.0\ucrt\stdio.h(208): note: 参见“fopen”的声明
tcpSocket.c
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\tcpsocket.c(74): warning C4133: “函数”: 从“sockaddr_in *”到“const sockaddr *”的类型不兼容
f:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\tcpsocket\tcpsocket.c(71): error C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
d:\windows kits\10\include\10.0.17763.0\um\winsock2.h(1831): note: 参见“inet_addr”的声明
正在生成代码...





+ 2
- 0
第二周/server/x64/Debug/server.tlog/server.lastbuildstate Dosyayı Görüntüle

@@ -0,0 +1,2 @@
#TargetFrameworkVersion=v4.0:PlatformToolSet=v141:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=10.0.17763.0
Debug|x64|F:\100 - 学习\190 - 研究生\1910 - 项目\1 - 803\socket文件传输 - w2版本\server\|




+ 121
- 0
第二周/tcpSocket/fileOperation.c Dosyayı Görüntüle

@@ -0,0 +1,121 @@
#include"fileOperation.h"
long g_fileSize; //保存文件大小,全局变量初始化为空
//long g_fileSize = 11152;
char *g_fileBuf; //保存文件数据
char g_recvBuf[1024]; //保存文件大小
//int main()


bool readFile(struct MsgHeader* pmsg)
{
FILE* read = fopen(pmsg->fileInfo.fileName, "rb"); //文件指针,rb为二进制方式打开
if (!read) //如果读取为空
{
perror("文件读取失败\n");
printf("找不到[%s]文件...\n", pmsg->fileInfo.fileName);
struct MsgHeader msg = { .msgID = MSG_OPENFILE_FAILD };
return false;
}

//获取文件大小,单位字节
fseek(read, 0, SEEK_END); //将文件位置指针移动到最后
g_fileSize = ftell(read); //ftell获取当前文件位置指针
fseek(read, 0, SEEK_SET); //移动到开头
printf("filesize:%d\n", g_fileSize);

//分配内存
g_fileBuf = calloc(g_fileSize, sizeof(char)); //calloc将内存全部初始化为0
if (!g_fileBuf)
{
return false;
}
//将文件读到内存中来
fread(g_fileBuf, sizeof(char), g_fileSize, read);

fclose(read);
return true;
}

bool sendFile(SOCKET s, struct MsgHeader* pmsg)
{
readFile(pmsg); //免去在server当中再读文件!!
//把文件名称和大小发给客户端
struct MsgHeader msg = { .msgID = MSG_FILESIZE,.fileInfo.fileSize = g_fileSize };
//fileName = C:\Users\Katherine\Desktop\TyporaHotKey.ahk
char tfname[200] = { 0 }, text[100]; //tfname文件名TyporaHotKey;text后缀名.ahk
_splitpath(pmsg->fileInfo.fileName, NULL, NULL, tfname, text); //分割路径
strcat(tfname, text); //拼接为TyporaHotKey.ahk,放入tfname
strcpy(msg.fileInfo.fileName, tfname); //拷贝到msg.fileInfo.fileName

//int ret1 = send(s, //发名称
int ret2 = send(s, (char*)&msg, sizeof(struct MsgHeader), 0); //发大小
//发送文件,【g_fileBuf】要改吗?应该不用
int ret = send(s, g_fileBuf, msg.fileInfo.fileSize,0); //实际发送的文件大小ret
if (ret == SOCKET_ERROR)
{
err("sendFile");
return false;
}
printf("发送成功(%d)Byte\n", ret);

return true;
}

void downloadFileName(SOCKET serfd)
{
char fileName[1024] = "你好,我是隔壁的泰山~";
gets_s(fileName, 1023); //获取要下载的文件

struct MsgHeader file = { .msgID = MSG_FILENAME };
strcpy(file.fileInfo.fileName, fileName);

send(serfd, (char*)&file, sizeof(struct MsgHeader), 0);

}

bool recvFile(SOCKET s)
{
//接收文件大小
recv(s, g_recvBuf, 1023, 0);
struct MsgHeader* msg = (struct MsgHeader*)g_recvBuf;
printf("接收的文件大小为:(%d)Byte\n", msg->fileInfo.fileSize);
//strcpy(msg->fileInfo.fileName, pmsg->fileInfo.fileName);
//接收文件时需要分配内存!!
if (g_fileBuf == NULL)
{
g_fileBuf = calloc(msg->fileInfo.fileSize, sizeof(char));
if (!g_fileBuf)
{
return false;
}
}
int ret = recv(s, g_fileBuf, msg->fileInfo.fileSize, 0);
//int ret = recv(s, g_fileBuf, 1023, 0);
if (ret == 0)
{
printf("服务器正常下线...\n");
}
else if (ret < 0)
{
err("recv");
}
saveFile(msg);
return true;
}

bool saveFile(struct MsgHeader* pmsg)
{
FILE* write = fopen(&(pmsg->fileInfo.fileName), "wb"); //文件指针,wb只允许写数据
if (!write) //如果读取为空
{
perror("文件读取失败\n");
return false;
}

fwrite(g_fileBuf, sizeof(char), pmsg->fileInfo.fileSize, write);

fclose(write);
return true;
}

+ 41
- 0
第二周/tcpSocket/fileOperation.h Dosyayı Görüntüle

@@ -0,0 +1,41 @@
#ifndef _FILEOPERATION_H_ //防止重复包含
#define _FILEOPERATION_H_
#include"tcpSocket.h"

//定义标记
enum MSGTAG
{
MSG_FILENAME = 1, //文件名
MSG_FILESIZE = 2, //文件大小
MSG_READY_READ = 3, //准备接受
MSG_SEND = 4, //发送
MSG_SUCCESSED = 5, //传输完成

MSG_OPENFILE_FAILD = 6 //告诉客户端文件找不到

};

struct MsgHeader //封装消息头结构体
{
enum MSGTAG msgID; //当前消息标记
struct
{
int fileSize; //文件大小
char fileName[256]; //文件名
}fileInfo;
};

/***服务器***/
//读取文件
bool readFile(struct MsgHeader* pmsg);
//发送文件
bool sendFile(SOCKET s, struct MsgHeader* pmsg);
/***客户端***/
//接收文件名
void downloadFileName(SOCKET serfd);
//接收文件
bool recvFile(SOCKET s);
//保存文件
bool saveFile(struct MsgHeader* pmsg);

#endif //!__FILEOP_H_

+ 84
- 0
第二周/tcpSocket/tcpSocket.c Dosyayı Görüntüle

@@ -0,0 +1,84 @@
#include"tcpSocket.h"

bool init_Socket()
{
WSADATA wsadata; //定义变量wsadata
if (0 != WSAStartup(MAKEWORD(2,2),&wsadata)) //WSA:windows socket ansyc windows异步套接字;2.2版本
{
//不等于0失败
//printf("[error]WSAStartup failed code %d", WSAGetLastError());
err("WSAStartup");
return false;
}

return true;
}

bool close_Socket()
{
if (0 != WSACleanup())
{
err("WSACleanup");
return false;
}

return true;
}

SOCKET create_serverSocket()
{
//1.创建一个空socket,AF_INET代表IPV4,SOCK_STREAM流式套接字
SOCKET fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == fd)
{
err("socket");
return INVALID_SOCKET; //错误就返回,正确就继续
}
//~0(对0取反) 对于有符号来说是-1,对于无符号来说是最大值

//2.给socket绑定本地的ip地址和端口号
struct sockaddr_in addr; //结构体addr
addr.sin_family = AF_INET; //协议族IPV4
addr.sin_port = htons(PORT); //本地字节序转成网络字节序,大端存储和小端存储的相互转换
addr.sin_addr.S_un.S_addr = ADDR_ANY; //绑定本地任意ip

if (SOCKET_ERROR == bind(fd, (struct sockaddr*)&addr, sizeof(addr))) //(struct sockaddr*)强制转换
{
err("bind");
return INVALID_SOCKET;
}
//3.开始监听
listen(fd, 10);

return fd;
}

SOCKET create_clientSocket(const char * ip)
{
//1.创建一个空socket,AF_INET代表IPV4,SOCK_STREAM流式套接字
SOCKET fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == fd)
{
err("socket");
return INVALID_SOCKET; //错误就返回,正确就继续
}
//~0(对0取反) 对于有符号来说是-1,对于无符号来说是最大值

//2.给socket绑定服务端的ip地址和端口号
struct sockaddr_in addr; //结构体addr
addr.sin_family = AF_INET; //协议族IPV4
addr.sin_port = htons(PORT); //本地字节序转成网络字节序,大端存储和小端存储的相互转换
addr.sin_addr.S_un.S_addr = inet_addr(ip); //绑定服务器ip,ip转换格式

//
if (INVALID_SOCKET == connect(fd, &addr, sizeof(addr)))
{
err("connect");
return INVALID_SOCKET;
}



return fd;
}


+ 21
- 0
第二周/tcpSocket/tcpSocket.h Dosyayı Görüntüle

@@ -0,0 +1,21 @@
#ifndef _TCPSOCKET_H_ //防止重复包含
#define _TCPSOCKET_H_
#include<stdbool.h>
#include<stdio.h> //printf
#include<WinSock2.h> //网络编程的头文件
#pragma comment(lib,"ws2_32.lib") //库文件

#define err(errMsg) printf("[error]%s failed code %d \
line:%d", errMsg, WSAGetLastError(),__LINE__); //宏定义print
#define PORT 8888 //设定端口号为8888;0-1024是系统保留,一般不用;宏定义不用分号

//初始化网络库
bool init_Socket(); //alt+enter
//关闭网络库
bool close_Socket();
//服务器:创建服务器socket(socket返回一个整数代表数组的下标)
SOCKET create_serverSocket();

//客户端:创建客户端socket,传入ip地址
SOCKET create_clientSocket(const char* ip);
#endif

Yükleniyor…
İptal
Kaydet