blah blah blah is here! blah blah » Close

up0down
link

Hello all
using the ff code I can establish connection to my PC from my partner's PC but not from my PC to his PC. When I try to connect it generates the following error message:
"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"
private void btnSend_Click(object sender, EventArgs e)
{
addres1 = IPAddress.Parse(txtHostName.Text);
end1 = new IPEndPoint(addres1, Convert.ToInt32(txtPort.Text));
m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
try
{
m_socClient.Connect(end1);
String szData = txtSendMes.Text; ;
byte[] byData = System.Text.Encoding.ASCII.GetBytes(szData);
m_socClient.Send(byData);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
help plz.
10Q.

last answered 2 years ago

1 answers

up0down
link

given you have a valid ip end point (valid ip address & port), a firewall is blocking the connection.

up0down
link

Thank u for your reply
Yep,I have used a valid valid ip address & port number.
so how can I resolve this?
10Q in advance!

up0down
link

the destination where you are connecting needs to open access to that port & IP Address on its firewall. how to do that depends on what you're connecting to (firewalls could be on the router, could be on the operating system, it could be a windows, or linux firewall, so on and so on).

up0down
link

Thank for your reply Mr. MadHatter
how can I open access of the port,IP Address on its firewall?( if the firewalls is on the operating system)
Thanks in advance!

up0down
link

don't know, but I know <a target="_new" href="http://www.google.com/search?q=windows+firewall%2C+open+ports">someone who might</a>

up0down
link

MadHatter Thank u very much
It works.
best regards

up0down
link

What about this MadHatter
I start developing a simple chat application using C#(socket programming). The server accepts the first sent message but not accept any further when a client re-sends.
How can I fix it?
I have used the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private AsyncCallback acceptCallBack;
private AsyncCallback receiveCallBack;
public Socket listenerSocket;
public Socket clientSocket;
public byte[] recv;
private void btnListen_Click(object sender, EventArgs e)
{
//CheckForIllegalCrossThreadCalls = false;
//acceptCallBack = new AsyncCallback(acceptHandler);
//listenerSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//IPEndPoint end1 = new IPEndPoint(IPAddress.Any, 8080);
//listenerSocket.Bind(end1);
//listenerSocket.Listen(10);
//listenerSocket.BeginAccept(acceptCallBack, null);
}
public void acceptHandler(IAsyncResult asyncResult)
{
receiveCallBack = new AsyncCallback(receiveHandler);
clientSocket = listenerSocket.EndAccept(asyncResult);
recv = new byte[1];
clientSocket.BeginReceive(recv, 0, 1,
SocketFlags.None, receiveCallBack, null);
}
public void receiveHandler(IAsyncResult asyncResult)
{
int bytesReceived = 0;
bytesReceived = clientSocket.EndReceive(asyncResult);
if (bytesReceived != 0)
{
txtReceived123.Text += Encoding.UTF8.GetString(recv);
recv = new byte[1];
clientSocket.BeginReceive(recv, 0, 1,
SocketFlags.None, receiveCallBack, null);
}
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
acceptCallBack = new AsyncCallback(acceptHandler);
listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint end1 = new IPEndPoint(IPAddress.Any, 8080);
listenerSocket.Bind(end1);
listenerSocket.Listen(10);
listenerSocket.BeginAccept(acceptCallBack, null);
}
}
}
10Qs

up0down
link

What about this MadHatter
I start developing a simple chat application using C#(socket programming). The server accepts the first sent message but not accept any further when a client re-sends.
How can I fix it?
I have used the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private AsyncCallback acceptCallBack;
private AsyncCallback receiveCallBack;
public Socket listenerSocket;
public Socket clientSocket;
public byte[] recv;
private void btnListen_Click(object sender, EventArgs e)
{
//CheckForIllegalCrossThreadCalls = false;
//acceptCallBack = new AsyncCallback(acceptHandler);
//listenerSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//IPEndPoint end1 = new IPEndPoint(IPAddress.Any, 8080);
//listenerSocket.Bind(end1);
//listenerSocket.Listen(10);
//listenerSocket.BeginAccept(acceptCallBack, null);
}
public void acceptHandler(IAsyncResult asyncResult)
{
receiveCallBack = new AsyncCallback(receiveHandler);
clientSocket = listenerSocket.EndAccept(asyncResult);
recv = new byte[1];
clientSocket.BeginReceive(recv, 0, 1,
SocketFlags.None, receiveCallBack, null);
}
public void receiveHandler(IAsyncResult asyncResult)
{
int bytesReceived = 0;
bytesReceived = clientSocket.EndReceive(asyncResult);
if (bytesReceived != 0)
{
txtReceived123.Text += Encoding.UTF8.GetString(recv);
recv = new byte[1];
clientSocket.BeginReceive(recv, 0, 1,
SocketFlags.None, receiveCallBack, null);
}
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
acceptCallBack = new AsyncCallback(acceptHandler);
listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint end1 = new IPEndPoint(IPAddress.Any, 8080);
listenerSocket.Bind(end1);
listenerSocket.Listen(10);
listenerSocket.BeginAccept(acceptCallBack, null);
}
}
}
10Qs

up0down
link

I've never seen an acceptable reason to use asynchronous sockets.
this sample is using .NET 2.0 anonymous methods & separate processing threads. you can just as easily break the anonymous method out into its own method. In windows forms you'll use the Form's Invoke method to pass the received text out of the processing thread into the UI thread by creating a delegate that takes a string parameter, then inside the processing thread (where I say Console.WriteLine) calling Invoke and passing the string to it, and inside the implementation method assigning the text to some textbox like you did in your sample.
<b>Server:</b>
<em>class Program {
static void Main(string[] args) {
using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
listener.Bind(new IPEndPoint(IPAddress.Any, 4282));
listener.Listen(int.MaxValue);
while (true) {
Socket client = listener.Accept();
Thread worker = new Thread(delegate() {
byte[] buffer = new byte[8192];
int count = 0;
List<byte> bytes = new List<byte>();
while (true) {
count = client.Receive(buffer);
if (count == buffer.Length) {
bytes.AddRange(buffer);
} else {
byte[] temp = new byte[count];
Array.Copy(buffer, 0, temp, 0, count);
bytes.AddRange(temp);
break;
}
}
string text = Encoding.UTF8.GetString(bytes.ToArray());
Console.WriteLine("Received: \"{0}\"", text);
});
worker.Start();
}
}
}
}</em>
<b>Client:</b>
<em>class Program {
static void Main(string[] args) {
string[] words = { "hello", "world", "test", "asdf" };
foreach (string word in words) {
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
client.Connect(new IPEndPoint(IPAddress.Loopback, 4282));
byte[] data = Encoding.UTF8.GetBytes(word);
client.Send(data);
}
}
Console.ReadLine();
}
}</em>

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback