Client与Server

监听套接字,通讯套接字,初始化网卡,多线程。想查自己的IP,ipconfig

服务端

#include "stdafx.h"
#include <WinSock2.h>  //套接字 socket  CS--->Client Server
#include <iostream>
#include <Windows.h>
using namespace std;

#pragma comment(lib,"ws2_32.lib")  //动态库的静态加载

DWORD WINAPI THreadFunction(LPVOID lParam);
SOCKET ClientSocket;
int main()
{
    //如果要使用ws2_32.lib模块下的函数时,要初始化该模块

    WSADATA wsaData = {0};
    WORD wVersion = MAKEWORD(2,2); //宏,第一个2放在低8位上,第二个放在高8位上。

    if(WSAStartup(wVersion,&wsaData)!=0)   //一参数为WORD类型,IN,需要赋好值传进来,二参数为LP指针,OUT,需要初始化,传进来接受数据
    {
        printf("Init Socket Error\r\n");
        return 0;
    }                                        //返回值为0,成功
                        //套接字相当于保安
    SOCKET ListenSocket;//监听套接字 Server socket大写为数据类型,小写为函数            (数据流)                                        (数据报)
    ListenSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); //创建监听套接字,第一参数服务于什么协议是TCP还是UDP,第二参数中SOCK_STREAM流传送适用于TCP,一个一个,SOCK_DGRAM报传送适用于UDP,一页一页的
    if(ListenSocket==INVALID_SOCKET) //创建失败,套接字不存在
    {
        WSACleanup();
        return 0;
    }
    //初始化网卡,相当于给保安建个房子
    SOCKADDR_IN  LocalAddress;
    LocalAddress.sin_family = AF_INET;                    //IP不要写死
    LocalAddress.sin_addr.S_un.S_addr = INADDR_ANY; //传IP地址的两种方式,直接强制转换为u_long型,或者像下面一样一个一个输
    LocalAddress.sin_port = htons(9627); // Host To net string  //端口连接,不能冲突(0~1024不要用,已被买断,60000多后面的端口也不要用,被QQ买断)

    /*
    LocalAddress.sin_addr.S_un.S.un_b.s_b1 = '192';
    LocalAddress.sin_addr.S_un.S.un_b.s_b2 = '168';
    LocalAddress.sin_addr.S_un.S.un_b.s_b3 = '1';
    LocalAddress.sin_addr.S_un.S.un_b.s_b4 = '100';
    */

    //绑定  让保安住到房子里      大数据小指针,解出三成员
    if(bind(ListenSocket,(sockaddr*)&LocalAddress,sizeof(SOCKADDR_IN))==SOCKET_ERROR)
    {
        closesocket(ListenSocket); //套接字存在,关闭套接字回收资源
        WSACleanup();
        return 0;
    }

    //监听
    listen(ListenSocket,3); //队列里面有多少未决的连接


    //Block 阻塞函数,信号不到达,得不到返回  TCP 上线请求
    SOCKADDR_IN RemoteAddress;               //客户端的IP地址
    int iRemoteAddressLen = sizeof(SOCKADDR_IN);
    ClientSocket = accept(ListenSocket,(sockaddr*)&RemoteAddress,&iRemoteAddressLen); //返回值为套接字

    //启动一个线程
    CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)THreadFunction,NULL,0,NULL);

    while(true)
    {
        char szBufferData[0x1000] = {0};
        scanf("%s",szBufferData);

        send(ClientSocket,szBufferData,strlen(szBufferData),0);
    }
    WSACleanup(); //库的回收
    return 0;
}

DWORD WINAPI THreadFunction(LPVOID lParam)
{
    while(true)
    {
        char szBufferData[0x1000] = {0};
        int iReturn = recv(ClientSocket,szBufferData,0x1000,0);
        szBufferData[iReturn] = '\0';
        printf("%s\r\n",szBufferData);
    }
}


客户端

#include "stdafx.h"
#include <WinSock2.h>  //套接字 socket 
#include <iostream>
#include <Windows.h>
using namespace std;

#pragma comment(lib,"ws2_32.lib")  //动态库的静态加载

DWORD WINAPI THreadFunction(LPVOID lParam);
SOCKET ClientSocket;//通讯套接字 Client 
int main()
{
    //如果要使用ws2_32.lib模块下的函数时,要初始化该模块

    WSADATA wsaData = {0};
    WORD wVersion = MAKEWORD(2,2); 

    if(WSAStartup(wVersion,&wsaData)!=0)  
    {
        printf("Init Socket Error\r\n");
        return 0;
    }                                        
                                                               
    ClientSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(ClientSocket==INVALID_SOCKET)  //创建通讯套接字
    {
        WSACleanup();
        return 0;
    }
    //初始化网卡
    SOCKADDR_IN  ServerAddress;
    ServerAddress.sin_family = AF_INET;
    ServerAddress.sin_addr.S_un.S_addr = inet_addr("192.168.0.102");    
    ServerAddress.sin_port = htons(9627);

    
    connect(ClientSocket,(sockaddr*)&ServerAddress,sizeof(SOCKADDR_IN)); //返回值为int

    CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)THreadFunction,NULL,0,NULL);

    while(true)
    {
        char szBufferData[0x1000] = {0};
        int iReturn = recv(ClientSocket,szBufferData,0x1000,0);
        szBufferData[iReturn] = '\0';
        printf("%s\r\n",szBufferData);
    }
    WSACleanup(); //库的回收
    return 0;
}


DWORD WINAPI THreadFunction(LPVOID lParam)
{
    while(true)
    {
        char szBufferData[0x1000] = {0};
        scanf("%s",szBufferData);

        send(ClientSocket,szBufferData,strlen(szBufferData),0);
    }
}

 

 








 

posted @ 2016-07-21 10:09  卷珠帘  阅读(539)  评论(0编辑  收藏  举报