mirror of
https://github.com/Kiritow/GSock.git
synced 2024-03-22 13:10:51 +08:00
Add getlocal,getpeer to class sock
This commit is contained in:
parent
0ed154eeff
commit
9996b38c19
22
gsock.cpp
22
gsock.cpp
|
@ -256,8 +256,28 @@ int sock::setrecvtime(int Second)
|
||||||
return setsockopt(sfd,SOL_SOCKET,SO_RCVTIMEO,(const char*)&outtime,sizeof(outtime));
|
return setsockopt(sfd,SOL_SOCKET,SO_RCVTIMEO,(const char*)&outtime,sizeof(outtime));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//forgive me, but writing code in hospital is really not a good experience.
|
||||||
|
int sock::getlocal(std::string& IPStr,int& Port)
|
||||||
|
{
|
||||||
|
struct sockaddr_in saddr;
|
||||||
|
socklen_t saddrlen=sizeof(saddr);
|
||||||
|
memset(&saddr,0,saddrlen);
|
||||||
|
int ret=getsockname(_pp->sfd,(sockaddr*)&saddr,&saddrlen);
|
||||||
|
IPStr=inet_ntoa(saddr.sin_addr);
|
||||||
|
Port=ntohs(saddr.sin_port);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sock::getpeer(std::string& IPStr,int& Port)
|
||||||
|
{
|
||||||
|
struct sockaddr_in saddr;
|
||||||
|
socklen_t saddrlen=sizeof(saddr);
|
||||||
|
memset(&saddr,0,saddrlen);
|
||||||
|
int ret=getpeername(_pp->sfd,(sockaddr*)&saddr,&saddrlen);
|
||||||
|
IPStr=inet_ntoa(saddr.sin_addr);
|
||||||
|
Port=ntohs(saddr.sin_port);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
struct serversock::_impl
|
struct serversock::_impl
|
||||||
{
|
{
|
||||||
|
|
3
gsock.h
3
gsock.h
|
@ -43,6 +43,9 @@ public:
|
||||||
int setsendtime(int Second);
|
int setsendtime(int Second);
|
||||||
int setrecvtime(int Second);
|
int setrecvtime(int Second);
|
||||||
|
|
||||||
|
int getlocal(std::string& IPStr,int& Port);
|
||||||
|
int getpeer(std::string& IPStr,int& Port);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sock(int);
|
sock(int);
|
||||||
friend class serversock;
|
friend class serversock;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user