Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
1
Answer
Need help C# WinSock TCP data recieve server!
Ryan Walsh
14y
5.7k
1
Reply
Hey everyone,
I am developing code to accept TCP communcations from one computer to another. Basically from this forum, all I need is the ability to send a data string to the other computer. here is the code I have to do so. they both are the same application, but can be set to server mode or client mode depending on which computer. you will see the differences in the "radiobutton" events below. basically the server is set to listen.
public
MainForm()
{
InitializeComponent();
tcpServer.LocalPort = 88;
tcpServer.DataArrival +=
new
AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEventHandler(tcpServer_DataArrival);
tcpServer.ConnectionRequest +=
new
AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEventHandler(tcpServer_ConnectionRequest);
tcpServer.ConnectEvent +=
new
EventHandler(tcpServer_ConnectEvent);
tcpServer.Error +=
new
AxMSWinsockLib.DMSWinsockControlEvents_ErrorEventHandler(tcpServer_Error);
tcpServer.Close();
this
.FormClosing +=
new
FormClosingEventHandler(MainForm_FormClosing);
}
private
void
tcpServer_Error(
object
sender, AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent e)
{
MessageBox.Show(e.description);
}
//added to close socket before program exit. this also is not working
//as subsequent calls to program returns "Address in use" error
private
void
MainForm_FormClosing(
object
sender, System.ComponentModel.CancelEventArgs e)
{
tcpServer.Close();
}
private
void
tcpServer_ConnectEvent(
object
sender, EventArgs e)
{
richTextBox1.AppendText(
"TCP Connection Made...\r\n"
);
try
{
tcpServer.SendData(
"Hey Ryan Here I am!"
);
//server does not receive this for some reason.
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
// tcpServer.Close();
}
private
void
tcpServer_ConnectionRequest(
object
sender, AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent e)
{
richTextBox1.AppendText(
"TCP Connection Requested...\r\n"
);
tcpServer.Close();
tcpServer.Accept(e.requestID);
isconnected =
true
;
tcpServer.SendData(
"1"
);
//client side will receive this message for some reason
}
private
void
tcpServer_DataArrival(
object
sender, AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent e)
{
richTextBox1.AppendText(
"Receiving TCP Data...\r\n"
);
string
inputstr=
""
;
object
data=(
object
)inputstr;
tcpServer.GetData(
ref
data);
richTextBox1.AppendText((
string
)data +
"\r\n"
);
if
(radioServer.Checked)
{
inputstr = (
string
)data;
ConnectModems();
sendingData = inputstr;
}
}
private
void
radioClient_CheckedChanged(
object
sender, EventArgs e)
{
if
(radioClient.Checked)
{
groupContacts.Visible =
false
;
groupTests.Visible =
false
;
tcpServer.Close();
lblRole.Text =
"CLIENT APP"
;
}
}
private
void
radioServer_CheckedChanged(
object
sender, EventArgs e)
{
if
(radioServer.Checked)
{
tcpServer.Close();
groupContacts.Visible =
true
;
groupTests.Visible =
true
;
tcpServer.Listen();
lblRole.Text =
"SERVER APP"
;
}
}
I have made the tcp connection, but calls to senddata() do not go through. please let me know what I am doing wrong! thanks!
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Creating an instance of the COM component with CLSID {E2ED7231-D342-11D3-BC59-0090279AB966} from the IClassFactory failed due to the following error: 80004005.
Is there a timeout or waiting time for Serial Ports?