C# 套接字无法发送数据
大家好,这个方法在客户端计算机上发送文件,
private void StartServer()
{
TcpListener lsn = new TcpListener(IPAddress.Any, 27015);
Socket sck;
try
{
while (true)
{
lsn.Start();
sck = lsn.AcceptSocket();
byte[] b = new byte[100];
int k = sck.Receive(b);
string recived = "";
for (int i = 0; i < k; i++)
{
recived = "" + recived + "" + Convert.ToChar(b[i]).ToString();
}
if (recived == "Version")
{
string _ipPort = sck.RemoteEndPoint.ToString();
var parts = _ipPort.Split(':');
_IPAddr = IPAddress.Parse(parts[0]);
_Port = Convert.ToInt32(parts[1]);
sck.Send(System.Text.Encoding.ASCII.GetBytes("1.1.0.0"));
}
k = sck.Receive(b);
recived = "";
for (int i = 0; i < k; i++)
{
recived = "" + recived + "" + Convert.ToChar(b[i]).ToString();
}
if (recived == "Update")
{
fName = "Cannonball.mp3";
byte[] fileName = Encoding.UTF8.GetBytes(fName);
byte[] fileData = File.ReadAllBytes("D:\\Cannonball.mp3");
byte[] fileNameLen = BitConverter.GetBytes(fileName.Length);
clientData = new byte[4 + fileName.Length + fileData.Length];
fileNameLen.CopyTo(clientData, 0);
fileName.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileName.Length);
if (sck.Connected == true)
{
sck.Send(clientData);
sck.Close();
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
当它到达最后一个 if 语句时不执行任何操作。我编写了这段代码
Socket sck1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sck1.Connect(_IPAddr, _Port);
sck1.Send(clientData);
,但 Visual Studio 给出了无法建立连接的错误。 我尝试了 999 端口,我知道该端口已打开 sck1.Connect(_IPAddr, 999);
并且客户端收到了文件。 有谁知道我如何将文件发送到服务器获得的远程端点(sck.RemoteEndPoint)?
Hello guys this method sending file on client machine
private void StartServer()
{
TcpListener lsn = new TcpListener(IPAddress.Any, 27015);
Socket sck;
try
{
while (true)
{
lsn.Start();
sck = lsn.AcceptSocket();
byte[] b = new byte[100];
int k = sck.Receive(b);
string recived = "";
for (int i = 0; i < k; i++)
{
recived = "" + recived + "" + Convert.ToChar(b[i]).ToString();
}
if (recived == "Version")
{
string _ipPort = sck.RemoteEndPoint.ToString();
var parts = _ipPort.Split(':');
_IPAddr = IPAddress.Parse(parts[0]);
_Port = Convert.ToInt32(parts[1]);
sck.Send(System.Text.Encoding.ASCII.GetBytes("1.1.0.0"));
}
k = sck.Receive(b);
recived = "";
for (int i = 0; i < k; i++)
{
recived = "" + recived + "" + Convert.ToChar(b[i]).ToString();
}
if (recived == "Update")
{
fName = "Cannonball.mp3";
byte[] fileName = Encoding.UTF8.GetBytes(fName);
byte[] fileData = File.ReadAllBytes("D:\\Cannonball.mp3");
byte[] fileNameLen = BitConverter.GetBytes(fileName.Length);
clientData = new byte[4 + fileName.Length + fileData.Length];
fileNameLen.CopyTo(clientData, 0);
fileName.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileName.Length);
if (sck.Connected == true)
{
sck.Send(clientData);
sck.Close();
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
When its goes to last if statment does not doing nothing. I wrote this code
Socket sck1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sck1.Connect(_IPAddr, _Port);
sck1.Send(clientData);
but visual studio gives error that cannot establish connection.
I tried 999 port which i knew was open sck1.Connect(_IPAddr, 999);
and client recieved file.
Does anyone know how i can send file that remote endpoint(sck.RemoteEndPoint) which server got ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
sck1.Connect(_IPAddr, 999)
连接到服务器计算机上的某个套接字,则至少_IPAddr
的值是正确的。当您尝试连接时,您是否检查过
_Port
的值?它与服务器正在侦听的端口号匹配吗?如果这些数字匹配,您可以尝试以下操作来测试您的服务器是否可访问:
If
sck1.Connect(_IPAddr, 999)
connects to some socket on the server's machine, then at least the value of_IPAddr
is correct.Did you check what value
_Port
has when you are trying to connect? Does it match the port number the server is listening at?If these numbers match, you could try the following to test if your server is reachable:
尝试使用 TCPClient 和 TCPListener 而不是套接字。
编辑:因为您在这里遇到不止一个问题。我决定向你们展示我是多么喜欢做这样的事情。
如果您需要的话,我会发布我的客户。 :)
祝你好运!
Try using TCPClient and TCPListener instead of Sockets.
EDIT: Since your having more than one problem here. I decided to show you how I like to do something like this.
I'll post my client if you need me to. :)
Good Luck!