socket communication experiment for non connection

Facing non connection is udp communication

Topology:

-------------------------------------------------------------------------------------------------------------

                                    Server<---------------------------->Client

                             127.0.0.1:8888

-------------------------------------------------------------------------------------------------------------

Learn about the steps of udp socker

Server:                                                                                                              

1. Set up datagram socket and return socket type s [socket()]

2. Bind socket s to local address (IP + port) [bind()]

3. Read / write data on socket ns until the end of [sendto()] [recvfrom()]

4. Close socket ns [closesocket()]

 

Client:

1. Set up datagram socket and return socket type s [socket()]

2. Read / write data on socket ns until the end of [sendto()] [recvfrom()]

3. Close socket ns [closesocket()]

Client does not need to bind address

-------------------------------------------------------------------------------------------------------------

vs2017 compiled

If the compilation fails, you can replace the old function name.

Server:

// Udp_server.cpp: defines the entry point for the console application.
//
//Related header file
#include "stdafx.h"
#include <WinSock2.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib,"WS2_32.lib")

int main()
{
	//Initialization
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
		printf("WSAStartup initialization failed");
		return 0;
	}

	//Variable declaration, assignment
	SOCKET RecvSocket;
	sockaddr_in RecvAddr;
	int Port = 8888;
	char RecvBuf[1024]; //Receive buffer
	char SendBuf[1024]; //Send buffer
	int BufLen = 1024;
	sockaddr_in SenderAddr;
	int SenderAddrSize = sizeof(SenderAddr);

	//Create socket to receive data
	RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

	//Binding ip and port
	RecvAddr.sin_family = AF_INET;
	RecvAddr.sin_port = htons(Port);
	RecvAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	bind(RecvSocket, (SOCKADDR *)&RecvAddr, sizeof(RecvAddr));

	
	printf("Server ready:\n");
	while (true) {

		//Receive
		int re_number = recvfrom(RecvSocket,
			RecvBuf,
			BufLen,
			0,
			(SOCKADDR *)&SenderAddr,
			&SenderAddrSize
		);
		if (re_number > 0) {
			printf("from client : %s\n", &RecvBuf);
			if (strcmp(RecvBuf, "quit") == 0)
			{
				closesocket(RecvSocket);
				return 0;
			}
		}

		//Send out
		gets_s(SendBuf); //Get keyboard input
		
		//Sending function
		int send_number = sendto(RecvSocket, 
			SendBuf, BufLen, 
			0, 
			(SOCKADDR *)&SenderAddr, 
			sizeof(SenderAddr)
		);
		printf("server_to_client: %s\n", SendBuf);
		if (strcmp(SendBuf, "quit") == 0){
			closesocket(RecvSocket);
			return 0;
		}
	}
		if (WSACleanup() == SOCKET_ERROR)
			printf("WSACleanup failed");
		return 0;
	
}

Client

// UDP? Client.cpp: defines the entry point for the console application.
//
//Related header file
#include "stdafx.h"
#include <WinSock2.h>
#include <stdio.h>
#include <iostream>
#Include < ws2tcpip. H > / / this header file is required for the function at line 35
using namespace std;
#pragma comment(lib,"WS2_32.lib")

int main()
{
	//Initialization
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
		printf("WSAStartup initialization failed");
		return 0;
	}

	//Variable declaration, assignment
	SOCKET SendSocket; 
	sockaddr_in RecvAddr; //Server address
	int Port = 8888;
	char SendBuf[1024];
	char RecvBuf[1024];
	int BufLen = 1024;
	
	//Create socket
	SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	
	//Set server address
	RecvAddr.sin_family = AF_INET;
	RecvAddr.sin_port = htons(Port);
	//Recvaddr.sin'addr.s'addr = inet'addr ("192.168.1.1"); / / vs2017 does not support this method
	InetPton(AF_INET, _T("127.0.0.1"), &RecvAddr.sin_addr.s_addr);
	int RecvAddrSize = sizeof(RecvAddr);
	//Send data to server
	printf("client ready:\n");
	
	while (true) {
		//Scanf_s (""% s ", sendbuf); / / if this function is used, the client will be sent a" hot "of the dead cycle for unknown reasons
		
		//Send out
		gets_s(SendBuf);
		int  st = sendto(SendSocket,
			SendBuf,
			BufLen,
			0,
			(SOCKADDR *)&RecvAddr,
			sizeof(RecvAddr)
			);
		if (strcmp(SendBuf, "quit") == 0) {
			closesocket(SendSocket);
			return 0;
		}
		
		//accept
		int recv_number = recvfrom(SendSocket, RecvBuf, BufLen, 0, (SOCKADDR *)&RecvAddr, &RecvAddrSize);
		printf("From server: %s\n" ,RecvBuf);
		if (strcmp(RecvBuf, "quit") == 0) {
			closesocket(SendSocket);
			return 0;
		}
		if (st == SOCKET_ERROR) {
			printf("send failed! \n");
			closesocket(SendSocket);
			WSACleanup();
			return -1;
		}
	}


}

Experimental results:

Keywords: socket

Added by Teaky on Thu, 26 Mar 2020 17:41:40 +0200