windowsmobile socket programming
Server Codeusing System;
using System.Threading;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");
counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
handleClinet client = new handleClinet();
client.startClient(clientSocket, Convert.ToString(counter));
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
}
//Class to handle each client request separatly
public class handleClinet
{
TcpClient clientSocket;
string clNo;
public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = 0;
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);
rCount = Convert.ToString(requestCount);
serverResponse = "Server to clinet(" + clNo + ") " + rCount;
sendBytes = Encoding.ASCII.GetBytes(serverResponse);
System.Data.DataSet ds = new System.Data.DataSet();
DataTable dt = new DataTable("Test");
dt.Columns.Add("fname");
dt.Columns.Add("lname");
dt.Rows.Add("Vishal", "Pawar");
dt.Rows.Add("Vishal", "Pawar");
dt.Rows.Add("Vishal", "Pawar");
ds.Tables.Add(dt);
BinaryFormatter bin = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
bin.Serialize(mem, ds);
byte[] b = mem.ToArray();
networkStream.Write(b, 0, b.Length);
networkStream.Flush();
Console.WriteLine(" >>>>>>>>>>>> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(" >> " + ex.ToString());
}
}
}
}
}
Client Code
using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Data;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
NetworkStream serverStream;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
msg("Client Started");
clientSocket.Connect("localhost", 8888);
label1.Text = "Client Socket Program - Server Connected ...";
}
private void button1_Click(object sender, EventArgs e)
{
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes("tttttttttttttttttt Message from Client$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
//System.Data.DataSet ds;
BinaryFormatter bf1 = new BinaryFormatter();
MemoryStream msnew = new MemoryStream();
msnew.Write(inStream, 0, inStream.Length);
msnew.Position = 0;
DataSet ds = (DataSet)bf1.Deserialize(msnew);
msg("Data from Server : " + ds.Tables["Test"].Rows[0]["fname"].ToString() + ds.Tables["Test"].Rows[0]["lname"].ToString());
}
public void msg(string mesg)
{
textBox1.Text = textBox1.Text + Environment.NewLine + " --> " + mesg;
}
}
}
5:09 AM
windowsmobile socket programming
Fuck you, this isn't windows mobile