复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304// file_client.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include "pch.h" #include <iostream> #include<WinSock2.h> #include<thread> #include<fstream> #pragma comment(lib,"ws2_32.lib") using namespace std; //=====================全局常量================ #define port 8901 #define client_ipadd "127.0.0.1" const int BUFFER_SIZE = 1024; //=====================全局变量================ SOCKET client; sockaddr_in saddr; sockaddr_in caddr; int slen = sizeof(saddr); int clen = sizeof(caddr); char buf[200]; char discon[] = "byebye"; char goon[] = "next"; ifstream fp; struct File { char buffer[BUFFER_SIZE]; int length; int flag; }file; //=====================函数声明================ void initSocket(); void getFileName(char path[], char name[]); void openFile(char path[]); int getFileLength(char buf[]); bool choose(); //DWORD WINAPI sendFile_Thread(LPVOID lp); void sendFile_Thread(); int main() { //初始化listenSer套接字 initSocket(); memset(buf, 0, sizeof(buf)); int irecv=recv(client, buf, sizeof(buf), 0); if (irecv == SOCKET_ERROR) { ::cout << "接收错误!" << endl; closesocket(client); WSACleanup(); exit(1); } ::cout <<buf<< endl; sendFile_Thread(); //开始发送文件 /*HANDLE hsend_Thread = CreateThread(NULL, 0, sendFile_Thread, (LPVOID)&file, 0, NULL); if (hsend_Thread == NULL) { cout << "线程创建失败" << endl; }*/ //CloseHandle(hsend_Thread); closesocket(client); WSACleanup(); system("pause"); return 0; } void initSocket() { WSADATA wsadata; WORD version = MAKEWORD(2, 2); if (::WSAStartup(version, &wsadata)) { ::cout << "加载Socket失败!" << endl; system("pause"); exit(1); } //创建socket client = socket(AF_INET, SOCK_STREAM, 0); if (client == INVALID_SOCKET) { ::WSACleanup(); system("pause"); exit(1); } //初始化地址包 saddr.sin_family = AF_INET; saddr.sin_port = htons(port); saddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //初始化变量 slen = sizeof(saddr); clen = sizeof(caddr); if (connect(client, (SOCKADDR*)&saddr, slen)==SOCKET_ERROR) { cout << "连接服务器失败!" << endl; closesocket(client); WSACleanup(); system("pause"); exit(1); } } // 判断文件夹是否存在 BOOL IsDirExist(char cdir[]) { string dir(cdir); size_t origsize = dir.length() + 1; const size_t newsize = 100; size_t convertedChars = 0; wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(dir.length() - 1)); mbstowcs_s(&convertedChars, wcstring, origsize, dir.c_str(), _TRUNCATE); DWORD dwAttrib = GetFileAttributes(wcstring); return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); } //打开文件函数声明,特别注意是传递引用,不然后续操作会出错 void openFile(char data[]) { while (1) { cout << "请输入要发送文件的路径: " << endl; cin >> data; //输入图片的绝对路径 // 以读 / 写方式打开一个二进制文件,只允许读 / 写数据。若图片无法打开,则路径有问题,关闭连接 fp.open(data, ios::in | ios::binary); if (!(fp.is_open())) { memset(data, 0, sizeof(data)); cout << "图片路径出错,请重新尝试!" << endl; } else { break; } } } void getFileName(char path[], char name[]) { int len = strlen(path); int count = 0; for (int i = len; i > 0; i--) { if (path[i] != '\') { count++; } else { break; } } int j = 0; int pos = len - count + 1; for (int i = pos; i < len; i++) { name[j++] = path[i]; } name[j] = ''; } int getFileLength(char buf[]) { FILE *p; int length = 0; if ( fopen_s(&p,buf, "rb+")) { //计算文件的总长度 fseek(p, 0, SEEK_END); //指针移动到图片的最后一个字节; length = ftell(p); //获取图片总长度 fseek(p, 0, SEEK_SET); //指针还原到开始位置 fclose(p); } else { ::cout << "文件打开错误" << endl; } return length; } bool choose() { int j; int isend; cout << endl; cout << "请选择是否继续发送图片:(1或者2)" << endl; cout << " 1: YES 2: NO " << endl; cout << "我的选择: "; cin >> j; if (j == 2) { isend = send(client, discon, strlen(discon), 0); if (isend == SOCKET_ERROR) { cout << "发送消息出错!" << endl; closesocket(client); WSACleanup(); system("pause"); exit(1); } cout << "Clent: " << discon << endl; cout << "关闭服务器连接!"; return FALSE; } else { isend = send(client, goon, strlen(goon), 0); if (isend == SOCKET_ERROR) { cout << "发送消息出错!" << endl; closesocket(client); WSACleanup(); system("pause"); exit(1); } cout << "Client: 请接受下一个文件。" << endl; return TRUE; } } void sendFile_Thread() { int len = sizeof(buf); int irecv = 0; int isend = 0; while (true) { cout << "==========================发送文件=========================" << endl; //打开文件 openFile(buf); ::cout << "buf=" <<buf<< endl; //获取发送图片的名字 char name[20] = ""; getFileName(buf, name); ::cout << "name=" << name << endl; //发送名字 isend = send(client, name, strlen(name), 0); if (isend == 0 || isend == SOCKET_ERROR) { ::cout << "发送文件名错误" << endl; closesocket(client); WSACleanup(); system("pause"); exit(1); } ::cout << "buf=" << buf << endl; // length = getFileLength(buf); streampos pos = fp.tellg(); fp.seekg(0,ios::end); cout << "1:" << fp.tellg() << endl; int length = fp.tellg(); fp.seekg(pos); cout << "2: length=" << length << endl; //分包发送图片,图片长度大于0,循环发送,否则发送完毕,停止发送 ::cout << endl; ::cout << "····准备发送····" << endl; //接收文件 while (length > 0) { memset(file.buffer, 0, BUFFER_SIZE); fp.read(file.buffer, sizeof(file.buffer)); int len = sizeof(file.buffer); if (length >= len) { file.length = len; file.flag = 0; } else { file.length = len; file.flag = 1; } isend = send(client, (char*)&file, sizeof(struct File), 0); if (isend == 0 || isend == SOCKET_ERROR) { ::cout << "发送文件出错" << endl; closesocket(client); WSACleanup(); fp.close(); system("pause"); exit(1); } else { length -= len; } } //接收完成标志 memset(buf, 0, sizeof(buf)); irecv = recv(client, buf, sizeof(buf), 0); if (irecv == 0 || irecv == SOCKET_ERROR) { cout << "接收文件失败!" << endl; cout << "断开连接!" << endl; closesocket(client); WSACleanup(); system("pause"); exit(1); } else { cout << "····传送成功····" << endl; cout << endl; } fp.close(); if (!choose()) break; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262// file_server.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include "pch.h" #include <iostream> #include<WinSock2.h> #include<thread> #include<fstream> #pragma comment(lib,"ws2_32.lib") using namespace std; //=====================全局常量================ #define port 8901 #define ser_ipadd "127.0.0.1" const int BUFFER_SIZE = 1024; //=====================全局变量================ SOCKET listenSer; SOCKET serClient; sockaddr_in saddr; sockaddr_in caddr; int slen = sizeof(saddr); int clen = sizeof(caddr); ofstream fp; char buf[200]; //char recvbuf[BUFFER_SIZE]; //char sendbuf[BUFFER_SIZE]; struct File { char buffer[BUFFER_SIZE]; int length; int flag; }file; //=====================函数声明================ void initSocket(); char* filePath(char name[]); void openFile(char path[]); //DWORD WINAPI recvFile_Thread(LPVOID lp); void recvFile_Thread(); int main() { //初始化listenSer套接字 initSocket(); //接收客户端的请求 serClient = ::accept(listenSer, (SOCKADDR*)&caddr, &clen); if (serClient == INVALID_SOCKET) { ::cout << "接收连接失败!" << endl; ::cout << WSAGetLastError() << endl; closesocket(serClient); WSACleanup(); system("pause"); exit(1); } else { ::cout << "····连接成功·····" << endl; if (send(serClient, "来自服务器:连接成功!", strlen("来自服务器:连接成功!"), 0) == SOCKET_ERROR) { cout << "发送失败!" << endl; } } recvFile_Thread(); //开始接收文件 /*HANDLE hrecv_Thread = CreateThread(NULL, 0, recvFile_Thread, (LPVOID)&file, 0, NULL); if (hrecv_Thread == NULL) { cout << "线程创建失败" << endl; }*/ //CloseHandle(hrecv_Thread); closesocket(serClient); closesocket(listenSer); WSACleanup(); system("pause"); return 0; } void initSocket(){ WSADATA wsadata; WORD version = MAKEWORD(2, 2); if (::WSAStartup(version, &wsadata)) { ::cout << "加载Socket失败!" << endl; system("pause"); exit(1); } //创建socket listenSer = socket(AF_INET, SOCK_STREAM, 0); if (listenSer == INVALID_SOCKET) { ::WSACleanup(); system("pause"); exit(1); } //初始化地址包 saddr.sin_family = AF_INET; saddr.sin_port = htons(port); saddr.sin_addr.S_un.S_addr = INADDR_ANY; //初始化变量 slen = sizeof(saddr); clen = sizeof(caddr); //绑定地址到服务器 int ret = ::bind(listenSer, (SOCKADDR*)&saddr, slen); if (ret == SOCKET_ERROR) { ::cout << "绑定地址失败" << endl; closesocket(listenSer); WSACleanup(); system("pause"); exit(1); } //监听连接请求,其实还是初始化socket内容 int li = ::listen(listenSer, 5); if (li == SOCKET_ERROR) { ::cout << "监听失败" << endl; ::cout << WSAGetLastError() << endl; closesocket(listenSer); WSACleanup(); system("pause"); exit(1); } } // 判断文件夹是否存在 BOOL IsDirExist(char cdir[]) { string dir(cdir); size_t origsize = dir.length() + 1; const size_t newsize = 100; size_t convertedChars = 0; wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(dir.length() - 1)); mbstowcs_s(&convertedChars, wcstring, origsize, dir.c_str(), _TRUNCATE); DWORD dwAttrib = GetFileAttributes(wcstring); return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); } char* filePath(char name[]) { char *dir; dir = new char[50]; while (true) { ::cout << "请输入图片存放路径: " << endl; cin >> dir; if (IsDirExist(dir)) { break; } else { ::cout << "文件目录不存在!" << endl; } } int nlen = strlen(name); int dlen = strlen(dir); for (int j = 0; j < nlen; j++) { dir[dlen] = name[j]; dlen += 1; } dir[dlen] = ''; return dir; } void openFile(char path[]) { // 以读 / 写方式打开或建立一个二进制文件,允许读和写。 fp.open(path, ios::out | ios::binary); if (!(fp.is_open())) { ::cout << "文件存放路径出错!" << endl; closesocket(serClient); WSACleanup(); system("pause"); exit(1); } ::cout << "存放图片的绝对路径: " << path << endl; ::cout << endl; } //DWORD WINAPI recvFile_Thread(LPVOID lp){ void recvFile_Thread() { int len = sizeof(buf); char end[] = "ok"; int irecv = 0; int isend = 0; while (true) { cout << "=======================================接收文件=====================================" << endl; //接收文件名 memset(buf, 0, len); irecv = recv(serClient, buf, len, 0); if (irecv == 0 || irecv == SOCKET_ERROR) { ::cout << "接收文件名错误" << endl; closesocket(serClient); WSACleanup(); system("pause"); exit(1); } ::cout << "接收的文件名:" << buf << endl; //打开文件 char *path; path=filePath(buf); openFile(path); cout << "····准备接收····" << endl; delete path; file.flag = 0; //接收文件 while (!file.flag) { memset(file.buffer, 0, BUFFER_SIZE); irecv = recv(serClient, (char*)&file, sizeof(struct File), 0); if (irecv == 0 || irecv == SOCKET_ERROR) { cout << "接收文件失败!" << endl; cout << "断开连接!" << endl; closesocket(serClient); WSACleanup(); system("pause"); exit(1); } fp.write(file.buffer, file.length); cout << "····接受中····" << endl; } //发送接收完成标志 isend = send(serClient, end, strlen(end), 0); if (isend == 0 || isend == SOCKET_ERROR) { cout << "发送确认信息失败!" << endl; cout << "断开连接!" << endl; closesocket(serClient); WSACleanup(); system("pause"); exit(1); } else { cout << "····接收完成····" << endl; } fp.close(); //接收是否继续传输的标志 memset(buf, 0, sizeof(buf)); irecv = recv(serClient, buf, sizeof(buf), 0); if (irecv == 0 || irecv == SOCKET_ERROR) { cout << "接收是否继续标志失败!" << endl; closesocket(serClient); WSACleanup(); system("pause"); exit(1); } if (strcmp(buf, "next") == 0) { cout << "准备接收下一个文件。" << endl; } else { cout << "关闭服务器连接" << endl; closesocket(serClient); WSACleanup(); system("pause"); exit(1); } } }
最后
以上就是瘦瘦星月最近收集整理的关于Socket-tcp-文件和图片的传送的全部内容,更多相关Socket-tcp-文件和图片内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复