Skip to content

Instantly share code, notes, and snippets.

@PifyZ
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PifyZ/d09fb961de5bda1439fe to your computer and use it in GitHub Desktop.
Save PifyZ/d09fb961de5bda1439fe to your computer and use it in GitHub Desktop.
Client/Serveur C

Le serveur attend une connexion et récupère les messages de cette connexion. Il fonctionne en arrière-plan sous le nom de "server.exe". Quand un message est reçu, il est affiché en bas à droite, dans la barre de notifications de Windows (là où l'heure est affichée).

Le client se connecte à une adresse IP écrite en brute dans le fichier "client.c".

A faire :

  • Gestion des erreurs :
    • Le serveur n'a pas pu se lancer
    • Le client n'a pas pu se connecter au serveur (il n'est sûrement pas lancé)
    • Le message envoyé par le client ne s'est pas envoyé
    • Autres ?
  • Possibilité de saisir l'adresse IP dans le programme mais avec une adresse IP par défaut si laissé vide.
  • Possibilité future de créer un chat

Compilation :

gcc server.c -o server.exe -lws2_32 -mwindows
gcc client.c -o client.exe -lws2_32
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#define SERVER_ADDR "192.168.0.50"
// #define SERVER_ADDR "127.0.0.1"
#define SERVER_PORT 9002
int main (int argc, char *argv[]) {
char buff[256];
WSADATA wsa;
WSAStartup(0x22, &wsa);
SOCKET connect_sock = INVALID_SOCKET;
connect_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN sock_addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = inet_addr(SERVER_ADDR),
.sin_port = htons(SERVER_PORT)
};
connect(connect_sock, (SOCKADDR *)&sock_addr, sizeof(sock_addr));
while (1) {
fgets(buff, sizeof(buff), stdin);
send(connect_sock, buff, strlen(buff) + 1, 0);
}
return 0;
}
#define _WIN32_IE 0x0501
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <shellapi.h>
#define PORT 9002
int main (int argc, char *argv[]) {
char buff[256];
int result = 0;
WSADATA wsa;
SOCKET listen_sock = INVALID_SOCKET;
SOCKET data_sock = INVALID_SOCKET;
NOTIFYICONDATA nid = {
.hWnd = GetDesktopWindow(),
.cbSize = sizeof(NOTIFYICONDATA),
.uFlags = NIF_INFO,
.szInfo = "",
.uTimeout = 10000,
.szInfoTitle = "Serveur",
.dwInfoFlags = NIIF_INFO
};
Shell_NotifyIcon(NIM_ADD, &nid);
WSAStartup(0x22, &wsa);
listen_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN sock_addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = INADDR_ANY,
.sin_port = htons(PORT)
};
bind(listen_sock, (SOCKADDR *)&sock_addr, sizeof(sock_addr));
while (1) {
listen(listen_sock, SOMAXCONN);
data_sock = INVALID_SOCKET;
data_sock = accept(listen_sock, NULL, NULL);
while (1) {
result = recv(data_sock, buff, sizeof(buff), 0);
if (result == SOCKET_ERROR || result == 0)
break;
buff[sizeof(buff) - 1] = '\0';
memcpy(nid.szInfo, buff, sizeof(buff));
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
closesocket(data_sock);
}
return 0;
}
@PifyZ
Copy link
Author

PifyZ commented Nov 8, 2014

Pour pouvez trouver Client/Serveur C sur Aylab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment