Results 1 to 4 of 4

Thread: TCP IP client and server no respond error

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2017
    Posts
    5
    Rep Power
    0

    Question TCP IP client and server no respond error

    i have a simple TCP/IP client and server that does not work: i want to use it to transfer data between some clients and a server enter image description here enter image description here


    on server side i have:


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Threading;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    using System.Collections;


    namespace TCP_IP_Server
    {
    public partial class frmMain : Form
    {
    private ArrayList nSockets;
    public frmMain()
    {
    InitializeComponent();
    }


    private void frmMain_Load(object sender, EventArgs e)
    {
    IPHostEntry IPhost = Dns.GetHostByName(Dns.GetHostName());
    lblStatus.Text = "IP Address: " + IPhost.AddressList[0].ToString();
    nSockets = new ArrayList();
    Thread thdListner = new Thread(new ThreadStart(listnerThread));
    }


    public void listnerThread()
    {
    TcpListener tcpListener = new TcpListener(8080);
    tcpListener.Start();
    while(true)
    {
    Socket handlerSocket = tcpListener.AcceptSocket();
    if(handlerSocket.Connected)
    {
    Control.CheckForIllegalCrossThreadCalls = false;
    lbConnections.Items.Add(handlerSocket.RemoteEndPoi nt.ToString() + " Connected.");
    lock (this)
    {
    nSockets.Add(handlerSocket);
    }
    ThreadStart thdstHandler = new ThreadStart(handlerThread);
    Thread thdHnadler = new Thread(thdstHandler);
    thdHnadler.Start();
    }
    }
    }


    public void handlerThread()
    {
    Socket handlerSocket = (Socket)nSockets[nSockets.Count - 1];
    NetworkStream networkStream = new NetworkStream(handlerSocket);
    int thisRead = 0;
    int BlockSize = 1024;
    byte[] dataByte = new byte[BlockSize];
    lock (this)
    {
    Stream fileStream = File.OpenWrite(@"%userprofile%\desktop\SubmitedFil e.txt");
    while(true)
    {
    thisRead = networkStream.Read(dataByte, 0, BlockSize);
    fileStream.Write(dataByte, 0, thisRead);
    if (thisRead == 0)
    break;
    }
    fileStream.Close();
    }
    lbConnections.Items.Add("File Written.");
    handlerSocket = null;
    }
    }
    }


    and on Client side:


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Threading;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;


    namespace TCP_IP_Client
    {
    public partial class frmMain : Form
    {
    public frmMain()
    {
    InitializeComponent();
    }


    private void btnBrowse_Click(object sender, EventArgs e)
    {
    ofdBrowse.ShowDialog();
    txtFile.Text = ofdBrowse.FileName;
    }


    private void btnSend_Click(object sender, EventArgs e)
    {
    Stream fileStream = File.OpenRead(txtFile.Text);
    byte[] fileBuffer = new byte[fileStream.Length];
    fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
    TcpClient tcp = new TcpClient(txtServer.Text, 8080);
    NetworkStream networkStream = tcp.GetStream();
    networkStream.Write(fileBuffer, 0, fileBuffer.GetLength(0));
    networkStream.Close();
    }
    }
    }


    i am running the server on a VPS that has static IP address and the client on my own pc, but after hitting send button an exception occurs:


    "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond **.**.**.**:8080"
    Last edited by Reza; 11-20-2018 at 2:29 AM.

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,979
    Rep Power
    154
    That code is all but unreadable. Don't use HTML tags if you're not posting HTML.

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2017
    Posts
    5
    Rep Power
    0
    sorry, i edited the post

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,979
    Rep Power
    154
    And now there's no formatting or indenting at all, so it's easier to read but still less that ideal. Please copy your code from the IDE paste it as plain text within APPROPRIATE tags, i.e.

    [code]your code here[/code]

    if you want to add extra formatting, e.g. bold or colour to highlight a line, and:

    [xcode=c#]your code here[/xcode]

    to add syntax highlighting automatically.

Similar Threads

  1. Replies: 0
    Last Post: 10-28-2018, 2:59 PM
  2. Question The SMTP server requires a secure connection or the client was not authenticated. Th
    By priteshjoshi in forum C# General Discussion
    Replies: 1
    Last Post: 08-25-2018, 4:31 AM
  3. Client Server Architecture
    By Viper92 in forum Windows Forms
    Replies: 0
    Last Post: 01-19-2016, 5:07 AM
  4. Replies: 1
    Last Post: 09-28-2015, 6:01 PM
  5. problem in Sending data from client to server
    By ayna in forum Windows Forms
    Replies: 1
    Last Post: 08-10-2014, 12:12 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •