no ale tu chyba kodu brakuje w tej prezentacji...
klient
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <intsafe.h>
#pragma comment(lib, "ws2_32.lib")
#define PORT 7777
#define TIMER_ID 1
#define TIMER_INTERVAL 500 // Timer interval in milliseconds (e.g., 1000 ms = 1 second)
HWND hInputField, hCaption, hCaption2, hCaption3;
HWND hTitle1, hTitle2, hTitle3, hTitle4;
int client_socket = INVALID_SOCKET; // Initialize the socket to INVALID_SOCKET
struct sockaddr_in server_address;
char buffer[1024] = {0};
char timestmp[300];
char elems[4];
int oo = 0;
char inf[40];
char numer;
void upd() {
char buffer[2]; // Buffer to hold a single character
// Update the first caption
buffer[0] = elems[0];
buffer[1] = '\0'; // Null-terminate the buffer
SetWindowText(hCaption, buffer);
// Update the second caption
buffer[0] = elems[1];
SetWindowText(hCaption2, buffer);
// Update the third caption
buffer[0] = elems[2];
SetWindowText(hCaption3, buffer);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch (Message) {
case WM_DESTROY: {
if (client_socket != INVALID_SOCKET) {
closesocket(client_socket);
}
WSACleanup();
PostQuitMessage(0);
break;
}
case WM_COMMAND:
if (LOWORD(wParam) == 1) {
char buffer2[256];
GetWindowText(hInputField, buffer2, sizeof(buffer2));
char z[10];
strcpy(z, "UP0");
z[2]=numer;
if (strlen(buffer2) > 0) {
// Append the first letter of buffer2 to z
strncat(z, buffer2, 1);
}
// if (client_socket == INVALID_SOCKET) {
client_socket = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(PORT);
if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == SOCKET_ERROR) {
MessageBox(NULL, "Connection to the server failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
closesocket(client_socket);
client_socket = INVALID_SOCKET;
return 0;
}
// }
char tab[30];
strcpy(tab, "UP00");
tab[2]=numer;
// MessageBox(NULL, tab, "Error", MB_ICONEXCLAMATION | MB_OK);
GetWindowText(hInputField, buffer2, sizeof(buffer2));
tab[3]=buffer2[0];
//tab[2]=numer+48;
//tab[3] = buffer2[0];
oo++;
if (oo == 9) {
oo = 0;
}
send(client_socket, tab, 4, 0);
// recv(client_socket, buffer, sizeof(buffer), 0);
/*
FILE *plik2 = fopen("app.txt","w");
fprintf(plik2,"%s",tab);
fclose(plik2);
*/
}
break;
case WM_TIMER:
if (wParam == TIMER_ID) {
// This code will be executed when the timer triggers
// Add your timer-related logic here
client_socket = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(PORT);
if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == SOCKET_ERROR) {
MessageBox(NULL, "Connection to the server failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
closesocket(client_socket);
client_socket = INVALID_SOCKET;
return 0;
}
char tab[30];
strcpy(tab, "check");
// tab[0] = oo + 48;
oo++;
if (oo == 9) {
oo = 0;
}
send(client_socket, tab, strlen("check"), 0);
recv(client_socket, buffer, sizeof(buffer), 0);
// MessageBox(NULL, inf, "Error", MB_ICONEXCLAMATION | MB_OK);
// MessageBox(NULL, buffer, "Error", MB_ICONEXCLAMATION | MB_OK);
char gg[500];
strcpy(gg,inf);
strncat(gg, "-", 1);strncat(gg, buffer,strlen(buffer));
//SetWindowText(hCaption2, "");
if(strncmp(inf,buffer,19)!=0){
// MessageBox(NULL, "inf", "Error", MB_ICONEXCLAMATION | MB_OK);
// MessageBox(NULL, buffer, "Error", MB_ICONEXCLAMATION | MB_OK);
strcpy(inf,buffer);
recv(client_socket, buffer, sizeof(buffer), 0);
SetWindowText(hCaption, "");
SetWindowText(hCaption2, "");
SetWindowText(hCaption3, "");
inf[22]='\0';
SetWindowText(hCaption3,&inf[21]);
inf[21]='\0';
SetWindowText(hCaption2,&inf[20]);
inf[20]='\0';
SetWindowText(hCaption,&inf[19]);
}
}
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WSADATA wsa;
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
FILE *plik = fopen("app.ini","r");
char c;
fscanf(plik,"%c",&c);
fclose(plik);
numer = c;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
MessageBox(NULL, "WSAStartup failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// MessageBox(NULL, (&char)numer, "Error", MB_ICONEXCLAMATION | MB_OK);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "WindowClass", &c, WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 440, 260, NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
} else {
hInputField = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER,
10, 10, 120, 25, hwnd, NULL, hInstance, NULL);
CreateWindowEx(0, "BUTTON", "Update Caption", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
150, 10, 120, 25, hwnd, (HMENU)1, hInstance, NULL);
};
hTitle1 = CreateWindowEx(0, "STATIC", "Klient 1", WS_CHILD | WS_VISIBLE,
10, 50, 120, 25, hwnd, NULL, hInstance, NULL);
hCaption = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE,
150, 50, 100, 25, hwnd, NULL, hInstance, NULL);
hTitle2 = CreateWindowEx(0, "STATIC", "Klient 2", WS_CHILD | WS_VISIBLE,
10, 100, 120, 25, hwnd, NULL, hInstance, NULL);
hCaption2 = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE,
150, 100, 100, 25, hwnd, NULL, hInstance, NULL);
hTitle3 = CreateWindowEx(0, "STATIC", "Klient 3", WS_CHILD | WS_VISIBLE,
10, 150, 120, 25, hwnd, NULL, hInstance, NULL);
hCaption3 = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE,
150, 150, 100, 25, hwnd, NULL, hInstance, NULL);
/*hTitle4 = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE,
10, 200, 250, 25, hwnd, NULL, hInstance, NULL); */
client_socket = INVALID_SOCKET; // Initialize the socket to INVALID_SOCKET
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(PORT);
SetTimer(hwnd, TIMER_ID, TIMER_INTERVAL, NULL);
while (GetMessage(&msg, NULL, 0, 0) > 0) {
// recv(client_socket, buffer, sizeof(buffer), 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (client_socket != INVALID_SOCKET) {
closesocket(client_socket);
}
WSACleanup();
return msg.wParam;
}
serwer
#include <winsock2.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#pragma comment(lib, "ws2_32.lib")
char elems[4];
char timestmp[300];
char oldtimestamp[300];
// Define the structure
struct res {
const char* res1;
char tmp[200];
const char* res3;
};
void getCurrentTimestamp() {
time_t currentTime;
struct tm* localTimeInfo;
// Get the current time
time(¤tTime);
// Convert the current time to local time
localTimeInfo = localtime(¤tTime);
// Format the timestamp
strftime(timestmp, sizeof(timestmp), "%Y-%m-%d %H:%M:%S", localTimeInfo);
printf("\n%s\n", timestmp);
}
// Create an instance of the structure
struct res tmp_res = {
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body>",
"", // Initialize tmp as an empty string
"</body></html>"
};
char httpResponse[2000];
int dl = 0;
int w = 0;
void load() {
// Clear httpResponse array
//memset(httpResponse, 0, sizeof(httpResponse));
// Copy res1 to httpResponse
strcpy(httpResponse + dl, tmp_res.res1);
dl += strlen(tmp_res.res1);
// Take input from the user for tmp_res.tmp
printf("Enter content for tmp_res.tmp: ");
scanf("%199s", tmp_res.tmp); // Limit input to 199 characters to leave space for null-terminator
// Copy tmp_res.tmp to httpResponse
strcpy(httpResponse + dl, tmp_res.tmp);
dl += strlen(tmp_res.tmp);
// Copy res3 to httpResponse
strcpy(httpResponse + dl, tmp_res.res3);
dl += strlen(tmp_res.res3);
dl++; // Add one extra byte (for null-terminator)
}
char inf[40];
DWORD WINAPI ThreadFunction(LPVOID lpParam) {
int port = *((int*)lpParam);
WSADATA wsaData;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "Failed to initialize Winsock for port %d.\n", port);
return 1;
}
// Create socket
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET) {
fprintf(stderr, "Failed to create socket for port %d.\n", port);
WSACleanup();
return 1;
}
// Set up the server address structure
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(port);
// Bind the socket
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
fprintf(stderr, "Failed to bind socket for port %d.\n", port);
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Server listening on port %d...\n", port);
do {
// Listen for incoming connections
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
fprintf(stderr, "Error listening on socket for port %d.\n", port);
closesocket(serverSocket);
WSACleanup();
return 1;
}
// Accept incoming connections
SOCKET clientSocket = accept(serverSocket, NULL, NULL);
if (clientSocket == INVALID_SOCKET) {
fprintf(stderr, "Error accepting connection on port %d.\n", port);
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Client connected on port %d.\n", port);
// Receive and print data
char buffer[1024];
int bytesRead = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesRead > 0) {
printf("Received data:\n%s\n---------\n", buffer);
if (port == 7777) {
if (buffer[0] == 'c') {
send(clientSocket, oldtimestamp, strlen(oldtimestamp), 0);
}
if (buffer[0] == 'U' && buffer[1] == 'P') {
getCurrentTimestamp();
strcpy(oldtimestamp, timestmp);
int y = buffer[2] - 48;
elems[y] = buffer[3];
printf("\n1-%c,2-%c,3-%c", elems[0], elems[1], elems[2]);
// Assuming oldtimestamp is a character array with enough space
oldtimestamp[19] = elems[0];
oldtimestamp[20] = elems[1];
oldtimestamp[21] = elems[2];
oldtimestamp[22] = '\0'; // Null-terminate the string
/* for (int i = 0; i < 3; i++) {
oldtimestamp[19 + i] = elems[i];
}*/
oldtimestamp[22] = '\0';
printf("\n\n-!- %s -changed!-\n\n", oldtimestamp);
}
} else if (port == 8080) {
if (buffer[0] == 'G') {
printf("Received 'G' on port 8080. Printing message.\n");
char tmp[500];
strcpy(tmp,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body>000</body></html>");
tmp[56]=elems[0];
tmp[57]=elems[1];
tmp[58]=elems[2];
send(clientSocket,tmp, strlen(tmp), 0);
}
}
buffer[bytesRead] = '\0';
}
// Cleanup for the current connection
closesocket(clientSocket);
printf("Client disconnected on port %d.\n", port);
} while (1);
// Cleanup (this part will never be reached in this example)
closesocket(serverSocket);
WSACleanup();
return 0;
}
int main() {
int port1 = 7777;
int port2 = 8080;
elems[0]=48;
elems[1]=48;
elems[2]=48;
// Create threads
HANDLE thread1 = CreateThread(NULL, 0, ThreadFunction, &port1, 0, NULL);
HANDLE thread2 = CreateThread(NULL, 0, ThreadFunction, &port2, 0, NULL);
if (thread1 == NULL || thread2 == NULL) {
fprintf(stderr, "Failed to create threads.\n");
return 1;
}
// Wait for threads to finish
WaitForSingleObject(thread1, INFINITE);
WaitForSingleObject(thread2, INFINITE);
// Close thread handles
CloseHandle(thread1);
CloseHandle(thread2);
return 0;
}