-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliente_gui.c
178 lines (133 loc) · 4.39 KB
/
cliente_gui.c
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
#include <windows.h>
#include <stdio.h>
#define ENVIAR 1
#define ABRIR 2
///////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include "ftp.c"
///////////////////////
char arquivo[512];
char ip[16];
char porta[6];
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
void AddMenus(HWND);
void AddControls(HWND);
HMENU hMenu;
HWND hIp;
HWND hPort;
HWND hArquivo;
////////////////////////////////////////
int remote_socket = 0;
int message_length = 0;
unsigned short remote_port = 0;
char remote_ip[32];
struct sockaddr_in remote_address;
WSADATA wsa_data;
////////////////////////////////////////
int msg_err_exit(char *msg) {
fprintf(stderr, msg);
return -1;
}
int cliente() {
if (WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0)
msg_err_exit("WSAStartup() failed\n");
remote_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (remote_socket == INVALID_SOCKET) {
WSACleanup();
msg_err_exit("socket() failed\n");
}
// preenchendo o remote_address (servidor)
memset(&remote_address, 0, sizeof(remote_address));
remote_address.sin_family = AF_INET;
remote_address.sin_addr.s_addr = inet_addr(ip);
remote_port = atoi(porta);
remote_address.sin_port = htons(remote_port);
printf("conectando ao servidor %s:%d\n", ip, remote_port);
if (connect(remote_socket, (struct sockaddr *) &remote_address,
sizeof(remote_address)) == SOCKET_ERROR) {
WSACleanup();
msg_err_exit("connect() failed\n");
}
if(send_file(remote_socket, arquivo) == SOCKET_ERROR){
WSACleanup();
closesocket(remote_socket);
msg_err_exit("Erro na transferencia do arquivo\n");
}
printf("encerrando\n");
WSACleanup();
closesocket(remote_socket);
return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow) {
WNDCLASSW wc = {0};
wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInst;
wc.lpszClassName = L"windowClass";
wc.lpfnWndProc = WindowProcedure;
if(!RegisterClassW(&wc))
return -1;
CreateWindow("windowClass", "File Sender", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 330, NULL, NULL, NULL, NULL);
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_SHOW);
MSG msg = {0};
while(GetMessage(&msg, NULL, (int) NULL, (int) NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void abrirArquivo(HWND hWnd) {
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = arquivo;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = 512;
ofn.lpstrFilter = "All files\0*.*\0";
ofn.nFilterIndex = 1;
GetOpenFileName(&ofn);
}
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
switch(msg) {
case WM_COMMAND:
switch(wp) {
case ENVIAR:
GetWindowText(hIp, ip, 16);
GetWindowText(hPort, porta, 6);
cliente();
////////////////////////////////////
// CHAMAR A FUNÇÃO DE ENVIAR AQUI //
////////////////////////////////////
break;
case ABRIR:
abrirArquivo(hWnd);
SetWindowText(hArquivo, arquivo);
break;
}
break;
case WM_CREATE:
AddControls(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd, msg, wp, lp);
}
}
void AddControls(HWND hWnd) {
CreateWindowW(L"Static", L"IP :", WS_VISIBLE | WS_CHILD | SS_CENTER, 110, 60, 25, 20, hWnd, NULL, NULL, NULL);
hIp = CreateWindowW(L"Edit", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 145, 60, 115, 20, hWnd, NULL, NULL, NULL);
CreateWindowW(L"Static", L"Porta :", WS_VISIBLE | WS_CHILD | SS_CENTER, 285, 60, 50, 20, hWnd, NULL, NULL, NULL);
hPort = CreateWindowW(L"Edit", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 335, 60, 45, 20, hWnd, NULL, NULL, NULL);
hArquivo = CreateWindowW(L"Static", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 60, 140, 255, 20, hWnd, NULL, NULL, NULL);
CreateWindowW(L"Button", L"Abrir Arquivo", WS_VISIBLE | WS_CHILD, 320, 140, 100, 20, hWnd, (HMENU) ABRIR, NULL, NULL);
CreateWindowW(L"Button", L"Enviar", WS_VISIBLE | WS_CHILD, 220, 220, 60, 20, hWnd, (HMENU) ENVIAR, NULL, NULL);
}
//////////////////////////////
//////////////////////////////