在服务器上的sock-client中获取客户端ip [英] getting client ip in sock-client on server

查看:201
本文介绍了在服务器上的sock-client中获取客户端ip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出的类代码是针对客户端连接的服务器

i想要获取已连接的客户端的IP地址

请帮助我如何获得

//听课

公共课听

{

TcpListener server = null;

线程tcpthread = null;

client [] cl = new client [3];


public listen()

{

//

// TODO:在这里添加构造函数逻辑

//

}

public void startlisten()

{

Int32 port = 3310;

IPAddress localAddr = IPAddress.Parse(" 192.168.0.6 ");


// TcpListener server = new TcpListener(port);

server = new TcpListener(localAddr,port);


//开始侦听客户端请求。

server.Start();

//进入监听循环。

for(int i = 0; i< 3; i ++)

{

cl [i] = new client();

cl [i] .status = true;

}


布尔标志;

而(true)

{

试试

{

flag = false;

//执行阻止调用以接受请求。

//你也可以用户server.AcceptSocket()这里。

for(int i = 0; i< 3; i ++)

{

if( cl [i] .status == true)

{

cl [i] =新客户端(server.AcceptTcpClient());

tcpthread = new Thread(new ThreadStart(cl [i] .getClient));

tcpthread.Start();

flag = true;

休息;

}

}

if(flag!= true)

{

//MessageBox.Show("所有客户忙碌);


}



}

catch(例外情况)

{


}

}


}

public void stoplisten()

{

server.Stop();

} < br $>
}


//客户端类

公共类客户端

{

TcpClient tcpClient;

public布尔状态;

//读取数据的缓冲区

Byte [] bytes = new Byte [256];

字符串数据= null;

IPAddress localAddr = IPAddress.Parse(" 192.168.0.6");


公共客户端( )

{//

// TODO:在这里添加构造函数逻辑

//

// status =是的;

}

公共客户端(TcpClient客户端)

{

tcpClient =客户端;

//

// TODO:在这里添加构造函数逻辑

//

status = false;

}

public void getClient()

{

试试

{

data = null;

//获取用于读写的流对象

NetworkStream stream = tcpClient.GetStream();

int i ;

//循环接收客户端发送的所有数据。

while((i = stream.Read(bytes,0,bytes.Length))!= 0)

{

//将数据字节转换为ASCII字符串。

data = System.Text.Encoding.ASCII.GetString(bytes,0,i);

//处理客户端发送的数据。 br />
string replyMsg = data;

clamdCommand x = new clamdCommand();

replyMsg = x.Command(replyMsg);

int

lowerport = int.Parse(ConfigurationSettings.AppSetti ngs.Get(" lowerport"));

int

upperport = int.Parse(ConfigurationSettings.AppSetti ngs.Get(" upperport"));

for(int y = lowerport; y< upperport; y ++)

{

if(replyMsg == y.ToString())

{

replyMsg = replyMsg +" \\\\\\ 0" ;;

byte [] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

//发送回复。

stream.Write(msg,0,msg.Length);

TcpListener listener = new TcpListener(localAddr,y);

listener.Start();

//用于读取数据的缓冲区

字节[] bs =新字节[256];

字符串d s = null;

//输入监听循环。

while(true)

{//执行阻止调用以接受请求。

//你也可以在这里用户server.AcceptSocket()。

TcpClient bufferclient = listener.AcceptTcpClient();

ds = null;

//获取一个用于读写的流对象

NetworkStream strm = bufferclient.GetStream();

int l;

//循环接收客户端发送的所有数据。

while((l = strm.Read(bs,0,bs.Length))!= 0)

{ds = System.Text.Encoding.ASCII.GetString(bs,0,l);

//处理客户发送的数据。

clamresults obj = new clamresults();

ds = obj.loadDatabase(" STREAM",ds);


replyMsg = ds;

bufferclient.Close();

休息;

}

listener.Stop();

x.freeport(y-lowerport);

休息;

}休息;

}


}

replyMs g = replyMsg +" \\\\\&& ;;

byte [] msgs = System.Text.Encoding.ASCII.GetBytes(replyMsg);

//发回回复。

stream.Write(msgs,0,msgs.Length);


}

}

catch(例外se)

{

MessageBox.Show(se.ToString());


}

//关机和结束连接

终于

{

tcpClient.Close() ;

status = true;

}

}


}

解决方案

DNS.Resolve()


它会给客户端的IP地址

i想要在客户的服务器上获取接受的IP地址或

连接

" Marko Becirevic" <毫安***************** @ inMail24.com>在消息中写道

news:dq ********** @ sunce.iskon.hr ...

DNS.Resolve()





" Ankit Aneja" < EF ***** @ newsgroups.nospam>在消息中写道

新闻:Oz ************** @ TK2MSFTNGP15.phx.gbl ...

它会给ip地址吗客户
我想在接受或连接的客户端服务器上获取IP地址



....


不,它不会。我在TcpClient文档中找不到任何东西(fw 1.1)

类可以揭示客户端的IP地址(但我可能在这里错了)。如果

你可以使用Socket类,那么你可以使用RemoteEndPoint属性。


问候,

Goran


The code of classes given below is for server to which clients connect
i want to get ip address of client which has connected
pls help how can i get
//listen class
public class listen
{
TcpListener server=null;
Thread tcpthread=null;
client[] cl=new client[3];

public listen()
{
//
// TODO: Add constructor logic here
//
}
public void startlisten()
{
Int32 port = 3310;
IPAddress localAddr = IPAddress.Parse("192.168.0.6");

// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);

// Start listening for client requests.
server.Start();
// Enter the listening loop.
for(int i=0;i<3;i++)
{
cl[i]=new client();
cl[i].status=true;
}

Boolean flag;
while(true)
{
try
{
flag=false;
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
for(int i=0;i<3;i++)
{
if(cl[i].status==true)
{
cl[i]= new client(server.AcceptTcpClient());
tcpthread=new Thread(new ThreadStart(cl[i].getClient));
tcpthread.Start();
flag=true;
break;
}
}
if(flag!=true)
{
//MessageBox.Show("All Clients Busy");

}


}
catch(Exception se)
{

}
}

}
public void stoplisten()
{
server.Stop();
}
}


//client class
public class client
{
TcpClient tcpClient;
public Boolean status;
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
IPAddress localAddr = IPAddress.Parse("192.168.0.6");

public client()
{ //
// TODO: Add constructor logic here
//
//status=true;
}
public client(TcpClient Client)
{
tcpClient =Client;
//
// TODO: Add constructor logic here
//
status=false;
}
public void getClient()
{
try
{
data = null;
// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetStream();
int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Process the data sent by the client.
string replyMsg = data;
clamdCommand x=new clamdCommand();
replyMsg=x.Command(replyMsg);
int
lowerport=int.Parse(ConfigurationSettings.AppSetti ngs.Get("lowerport"));
int
upperport=int.Parse(ConfigurationSettings.AppSetti ngs.Get("upperport"));
for(int y=lowerport;y<upperport;y++)
{
if(replyMsg==y.ToString())
{
replyMsg=replyMsg+"\r\n\0";
byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);
// Send back a response.
stream.Write(msg, 0, msg.Length);
TcpListener listener=new TcpListener(localAddr,y);
listener.Start();
// Buffer for reading data
Byte[] bs = new Byte[256];
String ds = null;
// Enter the listening loop.
while(true)
{ // Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient bufferclient = listener.AcceptTcpClient();
ds = null;
// Get a stream object for reading and writing
NetworkStream strm = bufferclient.GetStream();
int l;
// Loop to receive all the data sent by the client.
while((l = strm.Read(bs, 0, bs.Length))!=0)
{ ds = System.Text.Encoding.ASCII.GetString(bs, 0, l);
// Process the data sent by the client.
clamresults obj=new clamresults();
ds=obj.loadDatabase("STREAM",ds);

replyMsg=ds;
bufferclient.Close();
break;
}
listener.Stop();
x.freeport(y-lowerport);
break;
} break;
}

}
replyMsg=replyMsg+"\r\n\0";
byte[] msgs = System.Text.Encoding.ASCII.GetBytes(replyMsg);
// Send back a response.
stream.Write(msgs, 0, msgs.Length);

}
}
catch(Exception se)
{
MessageBox.Show(se.ToString());

}
// Shutdown and end connection
finally
{
tcpClient.Close();
status=true;
}
}

}

解决方案

DNS.Resolve()


will it give ip address of client
i want to fetch ip address on server of client which is accepted or
connected
"Marko Becirevic" <ma*****************@inMail24.com> wrote in message
news:dq**********@sunce.iskon.hr...

DNS.Resolve()




"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:Oz**************@TK2MSFTNGP15.phx.gbl...

will it give ip address of client
i want to fetch ip address on server of client which is accepted or
connected


....

No, it won''t. I can''t find anything in TcpClient documentation (fw 1.1)
class that would reveal client''s IP address (but I may be wrong here) . If
you can use Socket class, then you can use RemoteEndPoint property.

Regards,
Goran


这篇关于在服务器上的sock-client中获取客户端ip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆