windows客户端和linux虚拟机服务器实现简单socket通信

ubuntu命令行输入ifconfig查看ipwindows客户端和linux虚拟机服务器实现简单socket通信
服务器端代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main(){
    //创建套接字
    int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    //将套接字和IP、端口绑定
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));  //每个字节都用0填充
    serv_addr.sin_family = AF_INET;  //使用IPv4地址
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(1234);  //端口
    bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

    //进入监听状态,等待用户发起请求
    listen(serv_sock, 20);


    printf("waitting client...\n");
    //接收客户端请求
    struct sockaddr_in clnt_addr;
    socklen_t clnt_addr_size = sizeof(clnt_addr);
    int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
    if(-1==clnt_sock)
    {
	    printf("error\n");
	    return 2;
    }
    printf("connectc client success..\n");

    while(1)
    {
    //向客户端发送数据
    char str[100] = {0};
    printf("speak:");
    //scanf("%s",str);

    fgets(str,100,stdin);
    str[strlen(str)-1]='\0';
    write(clnt_sock, str, strlen(str));//向客户端发送数据
    //sleep(1);

    char getstr[100];
    memset(getstr,0,100);
    read(clnt_sock,getstr,100-1);
    printf("client:%s\n",getstr);
    
    
    }
    //关闭套接字
    close(clnt_sock);
    close(serv_sock);

    return 0;
}

windows客户端代码

//客户端
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS

#include<string.h>
#include <stdio.h>
#include <winsock2.h>
//#include <Windows.h>

#pragma comment(lib,"ws2_32.lib")

int main() {

	WORD ver = MAKEWORD(2,2);//版本
	WSADATA dat;
	WSAStartup(ver, &dat);

	if (HIBYTE(dat.wVersion) != 2 ||
		LOBYTE(dat.wVersion) != 2) {
			printf("请求版本失败!\n");
			WSACleanup();
			getchar();
			return -1;
	}
	printf("请求2.2版本成功!\n");


	//1.创建套接字
	SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == sock) {
		printf("创建socket失败!\n");
		WSACleanup();
		getchar();
		return -1;
	}
	printf("创建socket成功!\n");


	sockaddr_in sin = {};
	sin.sin_family = AF_INET;
	sin.sin_port = htons(1234);
	sin.sin_addr.S_un.S_addr = inet_addr("192.168.186.130");
	//连接
	if (SOCKET_ERROR == connect(sock, (sockaddr*)& sin, sizeof sin))
	{
		printf("连接失败!\n");
		getchar();
		return -1;
	}
	else
	{
		printf("连接服务器成功!\n");
	}

	//接受数据
	char str[100] = {0};
	char recvMsg[256] = {0};
	int r;
	r = recv(sock, recvMsg, 255, NULL);
	if (r > 0){
		printf("server:%s\n", recvMsg);
	}

	while (1) {

		memset(str, 0, NULL);
		printf("speak:");
		//scanf("%s", str);
		fflush(stdin);
		gets(str);
		if (strcmp("exit",str) == 0)
		{
			break;
		}
		send(sock, str, strlen(str), 0);
		memset(recvMsg, 0, NULL);
		r = recv(sock, recvMsg, 255, NULL);
		if (r > 0) {
			recvMsg[r] = '\0';
			printf("server:%s\n", recvMsg);	
		}
	}


	closesocket(sock);
	WSACleanup();

	printf("结束\n");
	getchar();
	return 0;
}

编译运行没问题
windows客户端和linux虚拟机服务器实现简单socket通信

上一篇:读书笔记_python网络编程3_(10)


下一篇:centos安装netcat TCP UDP测试工具 简称 nc,安全界叫它瑞士军刀