1. 程式人生 > >c++ Socket學習——The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name:

c++ Socket學習——The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name:

在寫c++伺服器時發現問題如下: 原始碼:

// ConsoleApplicationSock.cpp: 定義控制檯應用程式的入口點。

#include "stdafx.h"
#include <winsock.h>
#include <string.h>
#include <corecrt_io.h>
#pragma comment(lib,"ws2_32.lib")

int serverport = 5188;
//建立,繫結套接字,並開啟監聽
SOCKET InitialSocket()
{
	//建立套接字
	SOCKET listenfd = socket
(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listenfd == INVALID_SOCKET) { printf("socket error!"); } //繫結套接字,成功返回0,失敗返回-1 struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(serverport); servaddr.sin_addr.S_un.S_addr =
INADDR_ANY; if (bind(listenfd, (sockaddr*)&servaddr, sizeof(servaddr)) == SOCKET_ERROR) { printf("bind error!"); } //監聽,將listenfd轉換為被動套接字,SOMAXCONN為佇列最大值 //被動套接字:accept,主動套接字:connect if (listen(listenfd,SOMAXCONN)== SOCKET_ERROR) { printf("listen error!"); } return listenfd; } int main
() { //初始化WSA WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) { return 0; } SOCKET listenSock=InitialSocket(); //接受,成功返回非負整數,失敗返回-1 struct sockaddr_in peeraddr; int peerlen = sizeof(peeraddr); SOCKET connSock = accept(listenSock, (sockaddr*)&peeraddr, &peerlen); if (connSock < 0) { printf("accept socket error!"); } char recvbuf[1024]; while (1) { memset(recvbuf, 0, sizeof(recvbuf)); //下一句為出問題的程式碼 read(connSock,recvbuf,sizeof(recvbuf)); printf("%s",recvbuf); } return 0; }

編譯輸出…

1>------ 已啟動生成: 專案: ConsoleApplicationSock, 配置: Debug x64 ------ 1>stdafx.cpp 1>ConsoleApplicationSock.cpp 1>e:\aaa\1project\test\socktest\consoleapplicationsock\consoleapplicationsock\consoleapplicationsock.cpp(66): warning C4244: “引數”: 從“SOCKET”轉換到“int”,可能丟失資料 1>e:\aaa\1project\test\socktest\consoleapplicationsock\consoleapplicationsock\consoleapplicationsock.cpp(66): error C4996: ‘read’: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _read. See online help for details. 1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\ucrt\corecrt_io.h(523): note: 參見“read”的宣告 1>已完成生成專案“ConsoleApplicationSock.vcxproj”的操作 - 失敗。 ==========生成: 成功 0 個,失敗 1 個,最新 0 個,跳過 0 個 ==========

解決辦法: 進入“專案”=>“[專案名]屬性” 在這裡插入圖片描述 選擇“配置屬性”=>“C/C++”=>“前處理器”=>“前處理器定義”=>“編輯” 在這裡插入圖片描述 在定義框中新增

_CRT_NONSTDC_NO_DEPRECATE
_CRT_SECURE_NO_WARNINGS

在這裡插入圖片描述 點選“應用”,然後點“確定”,最後重新編譯,應該就可以了。