TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ryan Walsh
NA
1
5.7k
Need help C# WinSock TCP data recieve server!
Aug 13 2010 6:27 PM
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!
Reply
Answers (
1
)
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?