tcp/ip UDP 服务器、客户端的连接 14.5.14

SockCilent:

// SockClient.cpp : Defines the entry point for the console application.
//
// SockClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "conio.h"

//socket头文件
#include "winsock2.h"
//socket库的lib
#pragma comment(lib,"ws2_32.lib")

void TCPClient(){
    SOCKET sockclient=socket(AF_INET,SOCK_STREAM,
        IPPROTO_TCP);
    //判断是不是成功
    if(INVALID_SOCKET==sockclient){
        return;
    }
    //连接到服务器
    struct sockaddr_in addr={0};
    addr.sin_family=AF_INET;
    addr.sin_port=htons(5678);
    addr.sin_addr.S_un.S_addr=inet_addr("192.168.1.243");
    //连接服务器
   int s= connect(sockclient,(struct sockaddr *)&addr,sizeof(addr));
   if(s==0){
        printf("已经连接到服务器\n");
   }else{
        printf("服务器断开\n");
   }
    //数据的收发
   //向服务器发送数据
    char szSend[100]="hello server";
    send(sockclient,szSend,sizeof(szSend),0);
    //接受服务器发送数据
    char szText[100]={0};
    recv(sockclient,szSend,100,0);
    printf("%s\n",szSend);


    getch();
    //关闭SOCKET
    closesocket(sockclient);
}

void UDPClient(){
    //创建一个socket
    SOCKET sockclient=socket(AF_INET,
        SOCK_DGRAM,IPPROTO_UDP);
    if(INVALID_SOCKET==sockclient){
        printf("INVALID_SOCKET");
        return;
    }
    struct sockaddr_in svraddr={0};
    svraddr.sin_family=AF_INET;
    svraddr.sin_port=htons(5679);
    svraddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
    //数据收发
    char szSend[100]="Hello udp Server";
    sendto(sockclient,szSend,100,0,
        (struct sockaddr *)&svraddr,sizeof(svraddr));

    getch();
    //关闭SOCKET
    closesocket(sockclient);

}
int main(int argc, char* argv[])
{
    //初始化SOCKET库
    WSADATA wsa={0};
    WSAStartup(MAKEWORD(2,2),&wsa);
    //TCPClient();
    UDPClient();
    return 0;
}



SocketServer:

// SockClient.cpp : Defines the entry point for the console application.
//
// SockClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "conio.h"

//socket头文件
#include "winsock2.h"
//socket库的lib
#pragma comment(lib,"ws2_32.lib")

void TCPServer(){
    printf("开始等待客户端连接\n");
    //创建服务器的SOCKET
    SOCKET scoksvr=socket (AF_INET,SOCK_STREAM,IPPROTO_TCP);
    //绑定IP和端口
    struct sockaddr_in svraddr={0};
    svraddr.sin_family=AF_INET;
    //端口号
    svraddr.sin_port=htons(5678);
    //绑定的IP 下面设置的是当前机子IP
    svraddr.sin_addr.S_un.S_addr=htons(INADDR_ANY);
    bind(scoksvr,(struct sockaddr*)&svraddr,sizeof(svraddr));
    //侦听
    listen(scoksvr,SOMAXCONN);

    //等候客户端
    struct sockaddr_in clientaddr={0};
    int nLen=sizeof(clientaddr);
    SOCKET sockclient=accept(scoksvr,
        (struct sockaddr*)&clientaddr,&nLen);

    //数据收发

    char szText[100]={0};
    recv(sockclient,szText,100,0);
    printf("%s\n",szText);
    char szSend[100]="hello client";
    send(sockclient,szSend,sizeof(szSend),0);




    //printf("客户端已连接\n");
    getch();
    //关闭SOCKET
    closesocket(sockclient);
    closesocket(scoksvr);

}

void UDPServer(){
    printf("开始等待客户端连接\n");
    SOCKET socksvr=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    if(INVALID_SOCKET==socksvr){
        printf("INVALID_SOCKET\n");
    }
    //绑定
    struct sockaddr_in svraddr={0};
    svraddr.sin_family=AF_INET;
    svraddr.sin_port=htons(5679);
    //绑定本地的所有IP地址
    svraddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    bind(socksvr,(struct sockaddr*)&svraddr,sizeof(svraddr));
    //UPD数据接收
    char szRecv[100]={0};
    struct sockaddr_in clientaddr={0};
    int nLen=sizeof(clientaddr);
    recvfrom(socksvr,szRecv,100,0,
        (struct sockaddr*)&clientaddr,&nLen);

    
    printf("%s\n",szRecv);

    getch();

    //关闭
    closesocket(socksvr);
}
int main(int argc, char* argv[])
{
    //初始化SOCKET库
    WSADATA wsa={0};
    WSAStartup(MAKEWORD(2,2),&wsa);
    //服务器
 
    //TCPServer();
    UDPServer();
    return 0;
}

// SockClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "conio.h"

//socket头文件
#include "winsock2.h"
//socket库的lib
#pragma comment(lib,"ws2_32.lib")

void TCPServer(){
    printf("开始等待客户端连接\n");
    //创建服务器的SOCKET
    SOCKET scoksvr=socket (AF_INET,SOCK_STREAM,IPPROTO_TCP);
    //绑定IP和端口
    struct sockaddr_in svraddr={0};
    svraddr.sin_family=AF_INET;
    //端口号
    svraddr.sin_port=htons(5678);
    //绑定的IP 下面设置的是当前机子IP
    svraddr.sin_addr.S_un.S_addr=htons(INADDR_ANY);
    bind(scoksvr,(struct sockaddr*)&svraddr,sizeof(svraddr));
    //侦听
    listen(scoksvr,SOMAXCONN);

    //等候客户端
    struct sockaddr_in clientaddr={0};
    int nLen=sizeof(clientaddr);
    SOCKET sockclient=accept(scoksvr,
        (struct sockaddr*)&clientaddr,&nLen);

    //数据收发

    char szText[100]={0};
    recv(sockclient,szText,100,0);
    printf("%s\n",szText);
    char szSend[100]="hello client";
    send(sockclient,szSend,sizeof(szSend),0);




    //printf("客户端已连接\n");
    getch();
    //关闭SOCKET
    closesocket(sockclient);
    closesocket(scoksvr);

}

void UDPServer(){
    printf("开始等待客户端连接\n");
    SOCKET socksvr=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    if(INVALID_SOCKET==socksvr){
        printf("INVALID_SOCKET\n");
    }
    //绑定
    struct sockaddr_in svraddr={0};
    svraddr.sin_family=AF_INET;
    svraddr.sin_port=htons(5679);
    //绑定本地的所有IP地址
    svraddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    bind(socksvr,(struct sockaddr*)&svraddr,sizeof(svraddr));
    //UPD数据接收
    char szRecv[100]={0};
    struct sockaddr_in clientaddr={0};
    int nLen=sizeof(clientaddr);
    recvfrom(socksvr,szRecv,100,0,
        (struct sockaddr*)&clientaddr,&nLen);

    
    printf("%s\n",szRecv);

    getch();

    //关闭
    closesocket(socksvr);
}
int main(int argc, char* argv[])
{
    //初始化SOCKET库
    WSADATA wsa={0};
    WSAStartup(MAKEWORD(2,2),&wsa);
    //服务器
 
    //TCPServer();
    UDPServer();
    return 0;
}




你可能感兴趣的:(tcp/ip UDP 服务器、客户端的连接 14.5.14)