“An existing connection was forcibly closed by the remote host” Exception in WPF Application
I am trying to create network application in C# by establishing communication
by creating TCP server and client application.
But I am keep getting the Exception:
"An existing connection was forcibly closed by the remote host"
on the line:
int bytesReceived = clientsocket.Receive(dataRecieved);
Can Anyone help me resolve this error.
Here is My code:
Client Code:
namespace Client
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
string text;
public static Socket clientsocket;
public MainWindow()
InitializeComponent();
private void connectbutton_Click(object sender, RoutedEventArgs e)
IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
int port = 11000;
IPEndPoint remoteEndPoint = new IPEndPoint(ipaddress, port);
clientsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientsocket.Connect(remoteEndPoint);
private void search_btn_Click(object sender, RoutedEventArgs e)
text = ((TextBox)grid.FindName("search")).Text;
byte datasent = Encoding.ASCII.GetBytes(text);
int bytesent = clientsocket.Send(datasent);
byte dataRecieved = new byte[10 * 1024];
int bytesReceived = clientsocket.Receive(dataRecieved);
DataTable table = Deserailize(dataRecieved);
private static DataTable Deserailize(byte dataRecieved)
System.IO.MemoryStream stream = new System.IO.MemoryStream(dataRecieved);
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.
Serialization.Formatters.Binary.BinaryFormatter();
DataTable table = (DataTable)formatter.Deserialize(stream);
return table;
Server Code:
namespace Server
class Program
static void Main(string args)
IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
int port = 11000;
IPEndPoint localEndPoint = new IPEndPoint(ipaddress, port);
Socket connectionsocket = null;
try
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(localEndPoint);
serverSocket.Listen(1);
Console.WriteLine("Waiting For Connection");
connectionsocket = serverSocket.Accept();
Console.WriteLine("Connection Accepted");
while(true)
byte bytes = new byte[1024];
int byterecieved = connectionsocket.Receive(bytes);
string text = Encoding.ASCII.GetString(bytes, 0, byterecieved);
DataTable table = DB.getCustomer(text);
byte bytessent= serailize(table);
connectionsocket.Send(bytessent);
catch (Exception e)
private static byte serailize(DataTable table)
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.
Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(stream, table);
byte byteArray = stream.GetBuffer();
return byteArray;
DataBase class:
class DB
public static SqlConnection con = new SqlConnection();
public static string ConnectionString = @"Data Source=DESKTOP-9CG0F5H;Initial Catalog=worksheet3;Integrated Security=True";
static DB()
con = new SqlConnection(ConnectionString);
con.Open();
public static DataTable getCustomer(string text)
SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "Select * from Student where StudentName=@StudentName";
SqlDataAdapter adaptor = new SqlDataAdapter(command);
DataTable table = new DataTable();
adaptor.Fill(table);
return table;
}
c# wpf
add a comment |
I am trying to create network application in C# by establishing communication
by creating TCP server and client application.
But I am keep getting the Exception:
"An existing connection was forcibly closed by the remote host"
on the line:
int bytesReceived = clientsocket.Receive(dataRecieved);
Can Anyone help me resolve this error.
Here is My code:
Client Code:
namespace Client
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
string text;
public static Socket clientsocket;
public MainWindow()
InitializeComponent();
private void connectbutton_Click(object sender, RoutedEventArgs e)
IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
int port = 11000;
IPEndPoint remoteEndPoint = new IPEndPoint(ipaddress, port);
clientsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientsocket.Connect(remoteEndPoint);
private void search_btn_Click(object sender, RoutedEventArgs e)
text = ((TextBox)grid.FindName("search")).Text;
byte datasent = Encoding.ASCII.GetBytes(text);
int bytesent = clientsocket.Send(datasent);
byte dataRecieved = new byte[10 * 1024];
int bytesReceived = clientsocket.Receive(dataRecieved);
DataTable table = Deserailize(dataRecieved);
private static DataTable Deserailize(byte dataRecieved)
System.IO.MemoryStream stream = new System.IO.MemoryStream(dataRecieved);
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.
Serialization.Formatters.Binary.BinaryFormatter();
DataTable table = (DataTable)formatter.Deserialize(stream);
return table;
Server Code:
namespace Server
class Program
static void Main(string args)
IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
int port = 11000;
IPEndPoint localEndPoint = new IPEndPoint(ipaddress, port);
Socket connectionsocket = null;
try
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(localEndPoint);
serverSocket.Listen(1);
Console.WriteLine("Waiting For Connection");
connectionsocket = serverSocket.Accept();
Console.WriteLine("Connection Accepted");
while(true)
byte bytes = new byte[1024];
int byterecieved = connectionsocket.Receive(bytes);
string text = Encoding.ASCII.GetString(bytes, 0, byterecieved);
DataTable table = DB.getCustomer(text);
byte bytessent= serailize(table);
connectionsocket.Send(bytessent);
catch (Exception e)
private static byte serailize(DataTable table)
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.
Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(stream, table);
byte byteArray = stream.GetBuffer();
return byteArray;
DataBase class:
class DB
public static SqlConnection con = new SqlConnection();
public static string ConnectionString = @"Data Source=DESKTOP-9CG0F5H;Initial Catalog=worksheet3;Integrated Security=True";
static DB()
con = new SqlConnection(ConnectionString);
con.Open();
public static DataTable getCustomer(string text)
SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "Select * from Student where StudentName=@StudentName";
SqlDataAdapter adaptor = new SqlDataAdapter(command);
DataTable table = new DataTable();
adaptor.Fill(table);
return table;
}
c# wpf
Perhaps take a look at this SO question
– Cubemaster
Nov 14 '18 at 18:59
1
Possible duplicate of An existing connection was forcibly closed by the remote host
– Cubemaster
Nov 14 '18 at 19:00
add a comment |
I am trying to create network application in C# by establishing communication
by creating TCP server and client application.
But I am keep getting the Exception:
"An existing connection was forcibly closed by the remote host"
on the line:
int bytesReceived = clientsocket.Receive(dataRecieved);
Can Anyone help me resolve this error.
Here is My code:
Client Code:
namespace Client
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
string text;
public static Socket clientsocket;
public MainWindow()
InitializeComponent();
private void connectbutton_Click(object sender, RoutedEventArgs e)
IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
int port = 11000;
IPEndPoint remoteEndPoint = new IPEndPoint(ipaddress, port);
clientsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientsocket.Connect(remoteEndPoint);
private void search_btn_Click(object sender, RoutedEventArgs e)
text = ((TextBox)grid.FindName("search")).Text;
byte datasent = Encoding.ASCII.GetBytes(text);
int bytesent = clientsocket.Send(datasent);
byte dataRecieved = new byte[10 * 1024];
int bytesReceived = clientsocket.Receive(dataRecieved);
DataTable table = Deserailize(dataRecieved);
private static DataTable Deserailize(byte dataRecieved)
System.IO.MemoryStream stream = new System.IO.MemoryStream(dataRecieved);
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.
Serialization.Formatters.Binary.BinaryFormatter();
DataTable table = (DataTable)formatter.Deserialize(stream);
return table;
Server Code:
namespace Server
class Program
static void Main(string args)
IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
int port = 11000;
IPEndPoint localEndPoint = new IPEndPoint(ipaddress, port);
Socket connectionsocket = null;
try
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(localEndPoint);
serverSocket.Listen(1);
Console.WriteLine("Waiting For Connection");
connectionsocket = serverSocket.Accept();
Console.WriteLine("Connection Accepted");
while(true)
byte bytes = new byte[1024];
int byterecieved = connectionsocket.Receive(bytes);
string text = Encoding.ASCII.GetString(bytes, 0, byterecieved);
DataTable table = DB.getCustomer(text);
byte bytessent= serailize(table);
connectionsocket.Send(bytessent);
catch (Exception e)
private static byte serailize(DataTable table)
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.
Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(stream, table);
byte byteArray = stream.GetBuffer();
return byteArray;
DataBase class:
class DB
public static SqlConnection con = new SqlConnection();
public static string ConnectionString = @"Data Source=DESKTOP-9CG0F5H;Initial Catalog=worksheet3;Integrated Security=True";
static DB()
con = new SqlConnection(ConnectionString);
con.Open();
public static DataTable getCustomer(string text)
SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "Select * from Student where StudentName=@StudentName";
SqlDataAdapter adaptor = new SqlDataAdapter(command);
DataTable table = new DataTable();
adaptor.Fill(table);
return table;
}
c# wpf
I am trying to create network application in C# by establishing communication
by creating TCP server and client application.
But I am keep getting the Exception:
"An existing connection was forcibly closed by the remote host"
on the line:
int bytesReceived = clientsocket.Receive(dataRecieved);
Can Anyone help me resolve this error.
Here is My code:
Client Code:
namespace Client
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
string text;
public static Socket clientsocket;
public MainWindow()
InitializeComponent();
private void connectbutton_Click(object sender, RoutedEventArgs e)
IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
int port = 11000;
IPEndPoint remoteEndPoint = new IPEndPoint(ipaddress, port);
clientsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientsocket.Connect(remoteEndPoint);
private void search_btn_Click(object sender, RoutedEventArgs e)
text = ((TextBox)grid.FindName("search")).Text;
byte datasent = Encoding.ASCII.GetBytes(text);
int bytesent = clientsocket.Send(datasent);
byte dataRecieved = new byte[10 * 1024];
int bytesReceived = clientsocket.Receive(dataRecieved);
DataTable table = Deserailize(dataRecieved);
private static DataTable Deserailize(byte dataRecieved)
System.IO.MemoryStream stream = new System.IO.MemoryStream(dataRecieved);
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.
Serialization.Formatters.Binary.BinaryFormatter();
DataTable table = (DataTable)formatter.Deserialize(stream);
return table;
Server Code:
namespace Server
class Program
static void Main(string args)
IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
int port = 11000;
IPEndPoint localEndPoint = new IPEndPoint(ipaddress, port);
Socket connectionsocket = null;
try
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(localEndPoint);
serverSocket.Listen(1);
Console.WriteLine("Waiting For Connection");
connectionsocket = serverSocket.Accept();
Console.WriteLine("Connection Accepted");
while(true)
byte bytes = new byte[1024];
int byterecieved = connectionsocket.Receive(bytes);
string text = Encoding.ASCII.GetString(bytes, 0, byterecieved);
DataTable table = DB.getCustomer(text);
byte bytessent= serailize(table);
connectionsocket.Send(bytessent);
catch (Exception e)
private static byte serailize(DataTable table)
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.
Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(stream, table);
byte byteArray = stream.GetBuffer();
return byteArray;
DataBase class:
class DB
public static SqlConnection con = new SqlConnection();
public static string ConnectionString = @"Data Source=DESKTOP-9CG0F5H;Initial Catalog=worksheet3;Integrated Security=True";
static DB()
con = new SqlConnection(ConnectionString);
con.Open();
public static DataTable getCustomer(string text)
SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "Select * from Student where StudentName=@StudentName";
SqlDataAdapter adaptor = new SqlDataAdapter(command);
DataTable table = new DataTable();
adaptor.Fill(table);
return table;
}
c# wpf
c# wpf
asked Nov 14 '18 at 18:48
David GardnerDavid Gardner
1
1
Perhaps take a look at this SO question
– Cubemaster
Nov 14 '18 at 18:59
1
Possible duplicate of An existing connection was forcibly closed by the remote host
– Cubemaster
Nov 14 '18 at 19:00
add a comment |
Perhaps take a look at this SO question
– Cubemaster
Nov 14 '18 at 18:59
1
Possible duplicate of An existing connection was forcibly closed by the remote host
– Cubemaster
Nov 14 '18 at 19:00
Perhaps take a look at this SO question
– Cubemaster
Nov 14 '18 at 18:59
Perhaps take a look at this SO question
– Cubemaster
Nov 14 '18 at 18:59
1
1
Possible duplicate of An existing connection was forcibly closed by the remote host
– Cubemaster
Nov 14 '18 at 19:00
Possible duplicate of An existing connection was forcibly closed by the remote host
– Cubemaster
Nov 14 '18 at 19:00
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306900%2fan-existing-connection-was-forcibly-closed-by-the-remote-host-exception-in-wpf%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306900%2fan-existing-connection-was-forcibly-closed-by-the-remote-host-exception-in-wpf%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Perhaps take a look at this SO question
– Cubemaster
Nov 14 '18 at 18:59
1
Possible duplicate of An existing connection was forcibly closed by the remote host
– Cubemaster
Nov 14 '18 at 19:00