Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <winsock2.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #define PORT 2000
- int init_connection();
- int receive(int csock);
- int send_message(int csock);
- int main()
- {
- int socket = init_connection();
- int rest = 1;
- while(rest){
- rest = receive(socket);
- rest = send_message(socket);
- }
- return EXIT_SUCCESS;
- }
- int send_message(int csock){
- char bufferSend[50];
- int error;
- printf("<<<< SERVER DIT: ");
- fgets(bufferSend, sizeof(bufferSend), stdin);
- error = send(csock, bufferSend, strlen(bufferSend), 0);
- printf("\n...................Attente de la reponce du client (passiontez svp)....\n\n");
- if(error == SOCKET_ERROR){
- printf(":-/ Socket error, send !\n");
- return 0;
- }
- return 1;
- }
- int receive(int csock){
- char bufferRecv[50];
- int error;
- error = recv(csock, bufferRecv, sizeof(bufferRecv)-1, 0);
- if(error != SOCKET_ERROR){
- bufferRecv[error] = '\0';
- printf(">>>> CLIEN DIT: %s\n",bufferRecv);
- return 1;
- }else{
- printf(":-/ Socket error, recv !\n");
- return 0;
- }
- }
- int init_connection(){
- int error;
- WSADATA WSAData;
- WSAStartup(MAKEWORD(2,0), &WSAData);
- SOCKADDR_IN sin = {0};
- SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
- if( sock != INVALID_SOCKET ){
- printf(":-) Socket %d is now opened in TCP/IP mode.\n",sock);
- sin.sin_addr.s_addr = htonl(INADDR_ANY);
- sin.sin_family = AF_INET;
- sin.sin_port = htons(PORT);
- error = bind(sock, (SOCKADDR *)&sin, sizeof(sin));
- if( error != SOCKET_ERROR ){
- error = listen(sock, 2);
- if( error != SOCKET_ERROR ){
- printf(":-) listening on port %d\n"
- "Waiting for a client connection on this port...\n",PORT);
- SOCKADDR_IN csin = {0};
- int sizeofcsin = sizeof(csin);
- SOCKET csock = accept(sock, (SOCKADDR *)&csin, &sizeofcsin);
- if( csock != INVALID_SOCKET ){
- printf("::::: Client connected\n\n");
- return csock;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement