源碼如下:// 版本:1.0// 日期:2013.7.13// 更改日期:// 功能:基于TCP的Sockets編程// Win32Sock_service.cpp : 定義控制臺應用程序的入口點。// #include "stdafx.h"#include <WinSock2.h> //包含頭文件#include <stdio.h>#include <Windows.h>#pragma comment(lib, "WS2_32.lib") //顯式連接套接字庫int _tmain(int argc, _TCHAR* argv[]){WSADATA data; //定義WSADATA結構體對象WORD w = MAKEWORD(2,0); //定義版本號碼char sztext[] = "welcome !!\r\n"; //定義并初始化化發(fā)送到客戶端的字符串數(shù)組::WSAStartup(w, &data); //初始化套接字庫SOCKET s = NULL,s1 = NULL; //定義連接套接字和數(shù)據(jù)收發(fā)套接字句柄s = ::socket(AF_INET, SOCK_STREAM, 0); //創(chuàng)建TCP套接字sockaddr_in addr,addr2; //定義套接字地址結構int n =sizeof(addr2); //獲取套接字地址結構大小addr.sin_family = AF_INET; //初始化地址結構addr.sin_port = htons(1065);addr.sin_addr.S_un.S_addr = INADDR_ANY;::bind(s, (sockaddr*)&addr2,sizeof(addr)); //綁定套接字::listen(s, 5); //監(jiān)聽套接字printf("service is running\r\n");while(true){s1 = ::accept(s, (sockaddr*)&addr2, &n); //接受連接請求if (s1 != NULL){printf("%s have been connect now", inet_ntoa(addr2.sin_addr));::send(s1,sztext, sizeof(sztext), 0); //向客戶端發(fā)送字符串數(shù)組}//定義::closesocket(s); //關閉套接字句柄::closesocket(s1);::WSACleanup(); //釋放套接字庫if (getchar()){return 0;} else {::Sleep(100);}}}
請教下,我為什么獲取客戶端ip地址總是:204.204.204.204
拉莫斯之舞
2023-03-19 19:15:28