Click here to Skip to main content
15,441,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to send a file via tcp/ip.
I found this code here:
public  void Send(Socket socket, byte[] buffer, int offset, int size, int timeout)
       {

           int port = Convert.ToInt32(txtPort.Text);
           string host = txtIP.Text;

           IPHostEntry address = Dns.GetHostEntry(host);
           IPEndPoint ipe = new IPEndPoint(address.AddressList[0], port);
           int sent = 0;
           using (Socket sock = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
           {
               try
               {
                   sock.Connect(ipe);
                   MessageBox.Show("Conectado");
                   sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);

               }
               catch ( Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           }

But when i run the program i got an error:
No connection could be made because the target machine actively refused it


The other computer its say: it is connected to my PC, but nothing happens
I just want to send a file

What I have tried:

C#, TCP/IP, error trying to send a file from my pc to another
Posted
Updated 29-Jun-22 7:12am

1 solution

Do you have a listening "server" on the other side on the choosen port?
 
Share this answer
 
Comments
Luis M. Rojas 1-Jul-22 9:27am     CRLF
Well, i think YES. This is it what i want: I have a vitronic Machine which is running on a server (ip xxx.xxx.xxx, port xxx), this program send an answer in XML, the host (ip xxx.xxx.xxx, port yyy) has to read and send a XML and so on. I found this code to send and Recive, but nothing happen. private void startButton_Click(object sender, EventArgs e) { //IPEndPoint LocalEndpoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), 20000); IPEndPoint LocalEndpoint = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xx"), 20000); Socket _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _Socket.Bind(LocalEndpoint); _Socket.Listen(10); _Socket.BeginAccept(new AsyncCallback(Accept), _Socket); } private void Accept(IAsyncResult _IAsyncResult) { Socket AsyncSocket = (Socket)_IAsyncResult.AsyncState; Socket dataSocket = AsyncSocket.EndAccept(_IAsyncResult); var buffer = new byte[1024]; AsyncSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Receive), dataSocket); } private void Receive(IAsyncResult _IAsyncResult) { Socket AsyncSocket = (Socket)_IAsyncResult.AsyncState; AsyncSocket.EndReceive(_IAsyncResult); var buffer = new byte[1024]; var strReceive = Encoding.ASCII.GetString(buffer); txtXMLRecibido.Text +=strReceive; AsyncSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Receive), AsyncSocket); } private void button1_Click(object sender, EventArgs e) { //To conect to the server if (txtIP.Text == "") { lblMensaje.Text = "Debe Digitar el IP"; return; } if (txtPort.Text == "") { lblMensaje.Text = "Debe Digitar el número de Puerto"; return; } IPEndPoint RemoteEndPoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), Convert.ToInt32(txtPort.Text)); Socket _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _Socket.BeginConnect(RemoteEndPoint, new AsyncCallback(Connect), _Socket); } private void Connect(IAsyncResult _IAsyncResult) { try { Socket RemoteSocket = (Socket)_IAsyncResult.AsyncState; RemoteSocket.EndConnect(_IAsyncResult); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900