TCP IP client and server no respond error

Reza

Member
Joined
Oct 22, 2017
Messages
5
Programming Experience
1-3
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.RemoteEndPoint.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\SubmitedFile.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:
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.
 
Back
Top Bottom