首页 > 代码库 > windows 与 mac socket通信

windows 与 mac socket通信

#include <Winsock2.h>
#include <stdio.h>

void main()
{
    // 以下的几句都是固定的
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD( 1, 1 );

    err = WSAStartup( wVersionRequested, &wsaData );//该函数的功能是加载一个Winsocket库版本
    if ( err != 0 ) {
        return;
    }


    if ( LOBYTE( wsaData.wVersion ) != 1 ||
        HIBYTE( wsaData.wVersion ) != 1 ) {
            WSACleanup( );
            return; 
    }


    //真正socket编程部分
    SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);//面向连接的可靠性服务SOCK_STRAM

    SOCKADDR_IN addrSrv;//存放本地地址信息的
    addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//htol将主机字节序long型转换为网络字节序
    addrSrv.sin_family=AF_INET;
    addrSrv.sin_port=htons(6001);//htos用来将端口转换成字符,1024以上的数字即可

    bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//将socket绑定到相应地址和端口上

    listen(sockSrv,20);//等待队列中的最大长度为5

    SOCKADDR_IN addrClient;
    int len=sizeof(SOCKADDR);

    while(1)
    {
        SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);//建立一个新的套接字用于通信,不是前面的监听套接字
        char sendBuf[100];
        sprintf(sendBuf,"Server IP is:%s",
            inet_ntoa(addrClient.sin_addr));//inet_nota函数是将字符转换成ip地址

        char recvBuf[100];
        recv(sockConn,recvBuf,100,0);//服务器从客户端接受数据
        printf("%s\n",recvBuf);

        if(strcmp(recvBuf,"Reconnect")==0)
        {
            Sleep(5000);
            strcpy(sendBuf,"Finished");
            send(sockConn,sendBuf,strlen(sendBuf)+1,0);//服务器向客户端发送数据
        }

        


        closesocket(sockConn);//关闭socket
    }
}

 Mac 端

#include <netinet/in.h>    // for sockaddr_in
#include <sys/types.h>    // for socket
#include <sys/socket.h>    // for socket
#include <stdio.h>        // for printf
#include <stdlib.h>        // for exit
#include <string.h>        // for bzero
/*
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
 */

#define HELLO_WORLD_SERVER_PORT    6001
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512

int main(int argc, char **argv)
{

 
    //设置一个socket地址结构client_addr,代表客户机internet地址, 端口
    struct sockaddr_in client_addr;
    bzero(&client_addr,sizeof(client_addr)); //把一段内存区的内容全部设置为0
    client_addr.sin_family = AF_INET;    //internet协议族
    client_addr.sin_addr.s_addr = htons(INADDR_ANY);//INADDR_ANY表示自动获取本机地址
    client_addr.sin_port = htons(0);    //0表示让系统自动分配一个空闲端口
    //创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket
    int client_socket = socket(AF_INET,SOCK_STREAM,0);
    if( client_socket < 0)
    {
        printf("Create Socket Failed!\n");
        exit(1);
    }
    //把客户机的socket和客户机的socket地址结构联系起来
    if( bind(client_socket,(struct sockaddr*)&client_addr,sizeof(client_addr)))
    {
        printf("Client Bind Port Failed!\n");
        exit(1);
    }
 
    //设置一个socket地址结构server_addr,代表服务器的internet地址, 端口
    struct sockaddr_in server_addr;
    bzero(&server_addr,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    if(inet_aton("192.168.1.149",&server_addr.sin_addr) == 0) //服务器的IP地址来自程序的参数
    {
        printf("Server IP Address Error!\n");
        exit(1);
    }
    server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);
    socklen_t server_addr_length = sizeof(server_addr);
    //向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
    if(connect(client_socket,(struct sockaddr*)&server_addr, server_addr_length) < 0)
    {
        printf("Can Not Connect To %s!\n",argv[1]);
        exit(1);
    }
 
    
    char buffer[BUFFER_SIZE];
    bzero(buffer,BUFFER_SIZE);
    strcpy(buffer, "Reconnect");
    //向服务器发送buffer中的数据
    send(client_socket,buffer,BUFFER_SIZE,0);
 
    
    //从服务器接收数据到buffer中
    bzero(buffer,BUFFER_SIZE);
    int length = 0;
    if(recv(client_socket,buffer,BUFFER_SIZE,0)>0)
    {
        printf("Received %s\n",buffer);
    }
    //关闭socket
    close(client_socket);
    return 0;
}

 

windows 与 mac socket通信