-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
67 lines (55 loc) · 1.87 KB
/
client.c
File metadata and controls
67 lines (55 loc) · 1.87 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib") // Winsock kütüphanesi bağlantısı
#define PORT 8081
#define BUFFER_SIZE 1024
int main() {
WSADATA wsa;
SOCKET client_socket;
struct sockaddr_in server_address;
char buffer[BUFFER_SIZE];
// Winsock başlat
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("Winsock baslatilamadi. Hata kodu: %d\n", WSAGetLastError());
return 1;
}
// Soket oluştur
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Soket olusturulamadi. Hata kodu: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Sunucu adresini ayarla
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr);
// Sunucuya bağlan
if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) == SOCKET_ERROR) {
printf("Baglanti basarisiz. Hata kodu: %d\n", WSAGetLastError());
closesocket(client_socket);
WSACleanup();
return 1;
}
// Game loop
while (1) {
printf("Bir sayi tahmin edin (1-20): ");
fgets(buffer, BUFFER_SIZE, stdin);
buffer[strcspn(buffer, "\n")] = 0; // Yeni satır karakterini kaldır
// Gönderilen tahmin
send(client_socket, buffer, strlen(buffer), 0);
// Sunucudan yanıt al
memset(buffer, 0, BUFFER_SIZE);
int bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0);
if (bytes_received <= 0) {
printf("Sunucu ile baglanti kesildi.\n");
break;
}
printf("Sunucu: %s", buffer); // Sunucudan gelen mesajı yazdır
}
closesocket(client_socket);
WSACleanup();
return 0;
}