using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace ServerForAndroid
{
public partial class Form1 : Form
{
Thread t1;
String Rt;
static String ips;
static Int32 port = 9986;
delegate void SetTextCallback(string text);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (((Button)sender).Text == "Start Server")
{
((Button)sender).Text = "Stop Server";
this.richTextBox1.Text = "Server start...";
ips = textBox1.Text;
port = Int32.Parse(textBox2.Text);
t1 = new Thread(new ThreadStart(Server));
t1.SetApartmentState(ApartmentState.MTA);
t1.Start();
}
else
{
((Button)sender).Text = "Start Server";
t1.Interrupt();
this.richTextBox1.Text = "Server close...";
}
}
// si collega ad un server
public void Connect()
{
String message = "TmJoint";
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
TcpClient client = new TcpClient(ips, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Rt = message;
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Rt = responseData;
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Rt = e.Message;
}
catch (SocketException e)
{
Rt = e.Message;
}
Console.OpenStandardInput();
Console.Write(Rt);
}
// rimane in ascolto come un server
public void Server()
{
TcpListener server = null;
try
{
// Set the TcpListener on port 9986.
IPAddress localAddr = IPAddress.Parse(ips);
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
MessageServer(0,"Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
MessageServer(0,"Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
MessageServer(0,data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
MessageServer(1,data);
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
MessageServer(0,e.Message);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
MessageServer(0,"\nHit enter to continue...");
}
// filtro risposte ( gestione di action fuori da questo thread )
public void MessageServer(int action, string message)
{
int caseSwitch = action;
switch (caseSwitch)
{
case 0:
this.SetText(message);
break;
default:
MessageBox.Show(message);
break;
}
}
// pubblica il risultato nella finestra log
private void SetText(string text)
{
if (this.richTextBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.richTextBox1.Text = this.richTextBox1.Text + Environment.NewLine + text;
}
}
}
}