-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmywinsocketserver.cpp
More file actions
96 lines (88 loc) · 1.86 KB
/
mywinsocketserver.cpp
File metadata and controls
96 lines (88 loc) · 1.86 KB
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
#include<winsock2.h>
#include<windows.h>
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#pragma comment(lib,"ws2_32.lib")
#define DEFAULTPORT 8000
#define MAXLINE 4096
using namespace std;
char buff[4096];
int n;
FILE outfile;
void sendstr(string s)
{
send(sClient, (char *)s.data(), s.length(), 0);
}
void recvstr()
{
n = recv(sClient, buff, 0);
buff[n]='\0';
}
int main(int argc, char* argv[])
{
WORD sockVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(sockVersion, &wsaData) != 0)
{
return 0;
}
SOCKET slisten = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (slisten == INVALID_SOCKET)
{
cout<<"socket error !"<<endl;
return 0;
}
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(DEFAULTPORT);
servaddr.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
cout<<"bind error !"<<endl;
}
if(listen(slisten, 5) == SOCKET_ERROR)
{
cout<<"listen error !"<<endl;
return 0;
}
SOCKET sClient;
struct sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
cout<<"======waiting for connection======"<<endl;
while(1)
{
if(sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen) == INVALID_SOCKET)
{
cout<<"accept error !"<<endl;
continue;
}
cout<<"Connection built: "<<inet_ntoa(remoteAddr.sin_addr))<<endl;
recvstr();
int type=buff[0];
if(type=='f')
{
recvstr();
char path[1000];
strcpy(path,buff);
outfile=fopen(path,"w");
cout<<path<<endl;
}
recvstr();
while(buff[0]!='E'||buff[1]!='O'||buff[2]!='F')
{
cout<<"recv:"<<buff<<endl;
sendstr("Received\n");
if(type=='f') fprintf(outfile, "%s\n",buff);
recvstr();
}
sendstr("Fully received\n");
if(type=='f') fclose(outfile);
closesocket(sClient);
}
closesocket(slisten);
WSACleanup();
return 0;
}