| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2795 人关注过本帖
标题:[求助]请问socket编程里,服务器端用什么函数获取客户端的地址与端口
只看楼主 加入收藏
Samuel_D
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-11-8
收藏
 问题点数:0 回复次数:4 
[求助]请问socket编程里,服务器端用什么函数获取客户端的地址与端口
我是新手,请教高手们一下,socket编程里,服务器端用什么函数获取客户端的地址与端口,还请高手们不吝赐教!
搜索更多相关主题的帖子: 客户端的 socket 服务器 函数 端口 
2006-11-10 21:27
radcat
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:306
专家分:45
注 册:2006-9-12
收藏
得分:0 

仔细看吧,我自已看都头大

/接收函数部分

typedef struct _THREADDATA
{
SOCKET sock;
HWND hWnd;
} THREADDATA, *LPTHREADDATA;

DWORD WINAPI ClientThread(LPVOID lpParam)
{
SOCKET sock=((LPTHREADDATA)lpParam)->sock;
char szBuff[DEFAULT_BUFFER];
int ret,
nLeft,
idx;
char szMsg[255];
HWND hWnd = ((LPTHREADDATA)lpParam)->hWnd;

while(1)
{
// Perform a blocking recv() call
//
ret = recv(sock, szBuff, DEFAULT_BUFFER, 0);
if (ret == 0) // Graceful close
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "recv()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
szBuff[ret] = '\0';
wsprintf(szMsg,"%s", szBuff);

// add the szBuff text to ListBox
//
/*SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);*/
SetDlgItemText(hWnd,IDC_EDIT2,szMsg);

nLeft = ret;
idx = 0;
//
// Make sure we write all the data
//
while(nLeft > 0)
{
ret = send(sock, &szBuff[idx], nLeft, 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "send()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK |MB_ICONSTOP);
break;
}
nLeft -= ret;
idx += ret;
}
}
return 0;
}

//
// Function: Process
//
// Description:
// This function is called as a thread. It initialize
// Winsock, create the listening socket, bind to the
// local address, and wait for client connections.
//
DWORD WINAPI Process(LPVOID lpParam)
{
HWND hWnd = (HWND)lpParam;
WSADATA wsd;
char szMsg[255];
SOCKET sListen,
sClient;
int iAddrSize;
HANDLE hThread;
DWORD dwThreadId;
struct sockaddr_in local,
client;
THREADDATA data;

if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
MessageBox(hWnd, "不能加载Winsock!",
"", MB_OK | MB_ICONSTOP);
return 1;
}
// Create our listening socket
//
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sListen == SOCKET_ERROR)
{
wsprintf(szMsg, "socket()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
// Set the local interface and bind to it
//
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(DEFAULT_PORT);

if (bind(sListen, (struct sockaddr *)&local,
sizeof(local)) == SOCKET_ERROR)
{
wsprintf(szMsg, "bind()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
listen(sListen, 8);
//
// In a continous loop, wait for incoming clients. Once one
// is detected, create a thread and pass the handle off to it.
//
while (1)
{
iAddrSize = sizeof(client);
sClient = accept(sListen, (struct sockaddr *)&client,
&iAddrSize);
if (sClient == INVALID_SOCKET)
{
wsprintf(szMsg, "accept()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
wsprintf(szMsg, "已接受客户: %s:%d",
inet_ntoa(client.sin_addr), ntohs(client.sin_port));

// add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);

// set the ClientThread required data
//
data.hWnd = hWnd;
data.sock = sClient;

hThread = CreateThread(NULL, 0, ClientThread,
(LPVOID)&data, 0, &dwThreadId);
if (hThread == NULL)
{
wsprintf(szMsg, "CreateThread()失败: %d",
GetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
CloseHandle(hThread);
}
closesocket(sListen);

WSACleanup();
return 0;
}


//发送函数

DWORD WINAPI Processfrom(LPVOID lpParam)
{
HWND hWnd = (HWND)lpParam;
WSADATA wsd;
SOCKET sClient;
char szBuffer[DEFAULT_BUFFER];
int ret;
DWORD i;
struct sockaddr_in server;
struct hostent *host = NULL;
char szMsg[255];

if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
MessageBox(hWnd, "不能加载Winsock库!", "",
MB_OK | MB_ICONSTOP);
return 1;
}
//
// Create the socket, and attempt to connect to the server
//
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == INVALID_SOCKET)
{
wsprintf(szMsg, "socket()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
server.sin_family = AF_INET;
server.sin_port = htons(DEFAULT_PORT);
server.sin_addr.s_addr = inet_addr(SERVER);
//
// If the supplied server address wasn't in the form
// "aaa.bbb.ccc.ddd" it's a hostname, so try to resolve it
//
if (server.sin_addr.s_addr == INADDR_NONE)
{
host = gethostbyname(SERVER);
if (host == NULL)
{
wsprintf(szMsg, "不能解析服务器: %s", SERVER);
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
CopyMemory(&server.sin_addr, host->h_addr_list[0],
host->h_length);
}
if (connect(sClient, (struct sockaddr *)&server,
sizeof(server)) == SOCKET_ERROR)
{
wsprintf(szMsg, "connect()失败: %d", WSAGetLastError());
return 1;
}
// Send and receive data
//
for(i = 0; i < DEFAULT_COUNT; i++)
{
ret = send(sClient, DEFAULT_MESSAGE,
lstrlen(DEFAULT_MESSAGE), 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "send()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
wsprintf(szMsg, "发送了%d字节", ret);

// add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);

ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0);
if (ret == 0) // Graceful close
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "recv()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
szBuffer[ret] = '\0';
wsprintf(szMsg, "RECV [%d 字节]: '%s'", ret, szBuffer);

//add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);
}
closesocket(sClient);

WSACleanup();
return 0;
}


2006-11-11 08:50
Samuel_D
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-11-8
收藏
得分:0 
谢谢版主!能不能再问一下:函数inet_ntoa(struct )需要哪个头文件?
2006-11-11 09:28
luoxian_2003
Rank: 1
等 级:新手上路
威 望:2
帖 子:163
专家分:0
注 册:2006-2-22
收藏
得分:0 
好像是要引入库
Import Library: Link with ws2_32.lib.、
还有个#include <winsock2.h>这个也是必须的吧

天地有正气,凛烈万古存。
2006-11-12 12:37
Samuel_D
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-11-8
收藏
得分:0 
找到了,应该是&lt;arpa/inet.h&gt;,谢谢各位!
2006-11-12 13:02
快速回复:[求助]请问socket编程里,服务器端用什么函数获取客户端的地址与端口
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052478 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved