p2p connection works correctly only in one way

70 Views Asked by At

I'm trying to make my university client-server project for file sending with digital signs. For the file sending there's p2p connection should starts - the sender starts the host on its side and waiting for the reciever connection. It works correctly when I start client on the computer which is also used as a server and try to send files to another computer. But the reciever can't even connect when the sender is on non-server computer.

I tried different ways for sharing ips for p2p connection: to send the sender's RemoteEndPoint on the server side to both users, to send the sender's LocalEndPoint on its side to the reciever via the server and the first sender's ip address from the AddressList to the reciever via the server. All these methods worked when I send files from the client working on server's machine and didn't in other case. For the first method there was an error like "an attempt was made to perform an operation on a socket with the network disconnected" when the reciver tried to connect to the sender. And in both other methods there was message about unknown address to connect but it was local network ips. There's a part of the sender's code:

SignFunc();
_serverSocket.Send(Encoding.UTF8.GetBytes("/p2p/eom"));
SendAccept();//Recieving and processing acceprion for /p2p/eom
IPEndPoint ipEndPoint = new IPEndPoint(p2pAddress, 8080);
Socket socket = new Socket(p2pAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);//p2pAddress is getting from another thread from the server
socket.Bind(ipEndPoint);
socket.Listen(1000);
_serverSocket.Send(Encoding.UTF8.GetBytes(rec_name.Text));//rec_name - reciever's login
SendAccept();
rec_user = socket.Accept();
byte[] buffer = new byte[8192];
int bytesRec = rec_user.Receive(buffer);
string data = Encoding.UTF8.GetString(buffer, 0, bytesRec);
 
if (data.Contains("/ok"))
{
    rec_user.Send(Encoding.UTF8.GetBytes(userName.Text));
    rec_user.Receive(buffer);
    Thread p2p_thread = new Thread(p2p_listener);
    p2p_thread.IsBackground = true;
    p2p_thread.Start();
    SendSign(rec_user);
    p2p_thread.Abort();
}
 
socket.Close();

and the reciver's part of code

if (data.Equals("/p2p"))
{
    bytesRec = _serverSocket.Receive(buffer);
    data = Encoding.UTF8.GetString(buffer, 0, bytesRec);
    IPAddress ipAddress = IPAddress.Parse(data);//the same address that the sender gets
    IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 8080);
    Socket p2phost = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    p2phost.Connect(ipEndPoint);//there's the error appears
    DialogResult dialogResult = MessageBox.Show("Принять файл от пользователя?", "", MessageBoxButtons.YesNo);
 
    if (dialogResult == DialogResult.Yes)
    {
        bytesSent = p2phost.Send(Encoding.UTF8.GetBytes("/ok"));
        bytesRec = p2phost.Receive(buffer);
        string p2p_name = Encoding.UTF8.GetString(buffer, 0, bytesRec);
        bytesSent = p2phost.Send(Encoding.UTF8.GetBytes("/ok"));
        bytesRec = p2phost.Receive(buffer);
        GetFile(p2phost);
        bytesRec = p2phost.Receive(buffer);
        GetFile(p2phost);
        ApproveFunc(p2p_name, p2pfile_name);
     }
     else
     {
         bytesSent = p2phost.Send(Encoding.UTF8.GetBytes("/rej"));
     }
 
     p2phost.Close();
}

Both codes are for the first method. I've checked sending and recieving ips while debug - addresses are the same in all cases.

0

There are 0 best solutions below