zoukankan      html  css  js  c++  java
  • C++客户端程序(socket)

    // MyClient.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include "stdio.h"
    #include "Winsock2.h"
    #include "conio.h"
    #include "stdlib.h"
    #pragma comment(lib,"ws2_32.lib")

    void BeginSend(char *sendBuf)
    {
     WORD wVersionRequested;
     WSADATA wsaData;
     int err;

     wVersionRequested = MAKEWORD(1,1);

     err = WSAStartup(wVersionRequested,&wsaData);
     if(err!=0){
      exit(0);
     }
     if(LOBYTE(wsaData.wVersion)!=1||
      HIBYTE(wsaData.wVersion)!=1){
       WSACleanup();
       exit(0);
     }

     SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);

     SOCKADDR_IN addrSrv;
     addrSrv.sin_addr.S_un.S_addr=inet_addr("130.234.1.92");
     addrSrv.sin_family=AF_INET;
     addrSrv.sin_port=htons(5101);
     connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
    // char sendBuf[50];

        send(sockClient,sendBuf,strlen(sendBuf),0);

    /* char recvBuf[50];
     recv(sockClient,recvBuf,50,0);
     printf("%s ",recvBuf);*/

     closesocket(sockClient);
     WSACleanup();
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
     char sendBuf[100];
     while (true)
     {
      printf("Please input a value:");
      scanf("%s",sendBuf);
      BeginSend(sendBuf);
      printf("%s ",sendBuf);
     }
       
     return 0;
    }

  • 相关阅读:
    阅读笔记《梦断代码》其一
    第一次冲刺(第九天)
    第一次冲刺(第八天)
    第一冲刺阶段(第七天)
    第一冲刺阶段(第六天)
    第一冲刺阶段(第五天)
    MySQL数据库半同步复制
    MySQL事物
    MySQL字符编码
    MySQL用户授权
  • 原文地址:https://www.cnblogs.com/batman425/p/3167402.html
Copyright © 2011-2022 走看看