c#监听某一个端口的请求情况
c#监听某一个端口的请求情况,如80,请给出实现思想和主要技术。如有详细代码,另外加分。nksof,你的回答我在03下编译都通不过啊。还有我的项目是c/s的注意:80是已...
c#监听某一个端口的请求情况,如80,请给出实现思想和主要技术。如有详细代码,另外加分。
nksof,你的回答我在03下编译都通不过啊。还有我的项目是c/s的
注意:80是已在使用的端口,我是想监应这样的端口。所以nksof的答案是错的,谢谢!
wefgod3你好。你的程序也不对啊。换成80后抱错:
System.Net.Sockets.SocketException: 以一种访问权限不允许的方式做了一个访问套接字的尝试。 展开
nksof,你的回答我在03下编译都通不过啊。还有我的项目是c/s的
注意:80是已在使用的端口,我是想监应这样的端口。所以nksof的答案是错的,谢谢!
wefgod3你好。你的程序也不对啊。换成80后抱错:
System.Net.Sockets.SocketException: 以一种访问权限不允许的方式做了一个访问套接字的尝试。 展开
3个回答
展开全部
以一种访问权限不允许的方式做了一个访问套接字的尝试。
一般就是因为端口被占用
IPAddress Addr=IPAddress.Parse(this.hostname);
TcpListener server = new TcpListener(Addr,port);
server.Start();
-----------
如果你已在此地址上做了发送数据的socket或tcpclient或udp,那么这个监听和发送在同一个端口上就会发生错误的。
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MyTcpListener
{
public static void Main()
{
TcpListener server=null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while(true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.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);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
一般就是因为端口被占用
IPAddress Addr=IPAddress.Parse(this.hostname);
TcpListener server = new TcpListener(Addr,port);
server.Start();
-----------
如果你已在此地址上做了发送数据的socket或tcpclient或udp,那么这个监听和发送在同一个端口上就会发生错误的。
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MyTcpListener
{
public static void Main()
{
TcpListener server=null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while(true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.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);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
展开全部
try
{
listener = new TcpListener(80);
listener.Start();
Thread thread = new Thread(new ThreadStart(recieve));
thread.Start();
}
catch(Ececption ex)
{
MessageBox.Show(ex.Message)
}
privatge void recieve()
{
mySock = listener.AcceptSocket();
if(mySock.Connected)
{
while(!control)
{
netStream = new NetworkStream(mySock);
byte[] messageByte = new Byte[64];
netStream.Read(messageByte,0,messageByte.Length);
string readMessage = System.Text.Encoding.BigEndianUnicode.GetString(messageByte);
richTextBox1.AppendText(readMessage);
}
}
}
{
listener = new TcpListener(80);
listener.Start();
Thread thread = new Thread(new ThreadStart(recieve));
thread.Start();
}
catch(Ececption ex)
{
MessageBox.Show(ex.Message)
}
privatge void recieve()
{
mySock = listener.AcceptSocket();
if(mySock.Connected)
{
while(!control)
{
netStream = new NetworkStream(mySock);
byte[] messageByte = new Byte[64];
netStream.Read(messageByte,0,messageByte.Length);
string readMessage = System.Text.Encoding.BigEndianUnicode.GetString(messageByte);
richTextBox1.AppendText(readMessage);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请问楼主最后怎么解决的,监听一个已经使用了的端口,现在遇到了这个问题,希望楼主能够帮忙解答一下,邮箱zhoueh1991@163.com
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询