private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
button3->Enabled = "true";
timer1->Enabled = true;
timer = "True";
//--- INITIALIZATION -----------------------------------
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
textBox1->Text = "WSAStartup error :" + System::Convert::ToString(WSAGetLastError());
WSACleanup();
timer = false;
}
//------------------------------------------------------
//---- Build address structure to bind to socket.--------
memset(&channel, 0, sizeof(channel));// zerochannel
channel.sin_family = AF_INET;
channel.sin_addr.s_addr = htonl(INADDR_ANY);
channel.sin_port = htons(SERVER_PORT);
//--------------------------------------------------------
// ---- create SOCKET--------------------------------------
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s < 0) {
textBox1->Text = "socket error :" + System::Convert::ToString(WSAGetLastError());
WSACleanup();
timer = false;
}
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
//---------------------------------------------------------
//---- BIND socket ----------------------------------------
b = bind(s, (struct sockaddr *) &channel, sizeof(channel));
if (b < 0) {
textBox1->Text = "bind error :" + System::Convert::ToString(WSAGetLastError());
WSACleanup();
timer = false;
}
//----------------------------------------------------------
//---- LISTEN socket ----------------------------------------
l = listen(s, QUEUE_SIZE); // specify queue size
if (l < 0) {
textBox1->Text = "listen error %ld" + System::Convert::ToString(WSAGetLastError());
WSACleanup();
timer = false;
}
//-----------------------------------------------------------
//---- ACCEPT connection ------------------------------------
sa = accept(s, 0, 0); // block for connection request
if (sa < 0) {
textBox1->Text = "accept error " + System::Convert::ToString(WSAGetLastError());
WSACleanup();
timer = false;
}
else {
textBox1->Text = "connection accepted";
}
//------------------------------------------------------------
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
timer = false;
closesocket(s);
closesocket(sa);
WSACleanup();
timer1->Enabled = FALSE;
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
// ---- RECV bytes --------------------------------------------
unsigned long mode = 1;
ioctlsocket(sa, FIONBIO, &mode);
bytesRecv = recv(sa, recvbuf, 50, 0);
err = WSAGetLastError();// 10057 = A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call)
if (bytesRecv == 0 || bytesRecv == WSAECONNRESET) {
textBox1->Text = "Connection Closed.\n";
WSACleanup();
}
else{
textBox1->Text = gcnew String(recvbuf);
}
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
char buf[50] = "";
int bytesSent;
if (timer == true) {
String ^ strT;
strT = textBox2->Text;
int TempNumOne = textBox2->Text->Length;
for (int a = 0; a < TempNumOne; a++)
{
buf[a] = strT[a];
}
bytesSent = send(s, buf, 50, 0);
textBox1->Text = "Sent:" + System::Convert::ToString(bytesSent);
textBox1->Text = "Message :" + textBox2->Text; }
}
}
ioctlsocket(sa, FIONBIO, &mode);
bytesRecv = recv(sa, recvbuf, 50, 0);
err = WSAGetLastError();// 10057 = A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call)
if (bytesRecv == 0 || bytesRecv == WSAECONNRESET) {