/* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765 */ using System; using System.Text; using System.Net; using System.Net.Sockets; public class EmployeeSrvr { public static void Main() { byte[] data = new byte[1024]; TcpListener server = new TcpListener(9050); server.Start(); TcpClient client = server.AcceptTcpClient(); NetworkStream ns = client.GetStream(); byte[] size = new byte[2]; int recv = ns.Read(size, 0, 2); int packsize = BitConverter.ToInt16(size, 0); Console.WriteLine("packet size = {0}", packsize); recv = ns.Read(data, 0, packsize); Employee emp1 = new Employee(data); Console.WriteLine("emp1.EmployeeID = {0}", emp1.EmployeeID); Console.WriteLine("emp1.LastName = {0}", emp1.LastName); Console.WriteLine("emp1.FirstName = {0}", emp1.FirstName); Console.WriteLine("emp1.YearsService = {0}", emp1.YearsService); Console.WriteLine("emp1.Salary = {0} ", emp1.Salary); size = new byte[2]; recv = ns.Read(size, 0, 2); packsize = BitConverter.ToInt16(size, 0); data = new byte[packsize]; Console.WriteLine("packet size = {0}", packsize); recv = ns.Read(data, 0, packsize); Employee emp2 = new Employee(data); Console.WriteLine("emp2.EmployeeID = {0}", emp2.EmployeeID); Console.WriteLine("emp2.LastName = {0}", emp2.LastName); Console.WriteLine("emp2.FirstName = {0}", emp2.FirstName); Console.WriteLine("emp2.YearsService = {0}", emp2.YearsService); Console.WriteLine("emp2.Salary = {0}", emp2.Salary); ns.Close(); client.Close(); server.Stop(); } } public class Employee { public int EmployeeID; private int LastNameSize; public string LastName; private int FirstNameSize; public string FirstName; public int YearsService; public double Salary; public int size; public Employee() { } public Employee(byte[] data) { int place = 0; EmployeeID = BitConverter.ToInt32(data, place); place += 4; LastNameSize = BitConverter.ToInt32(data, place); place += 4; LastName = Encoding.ASCII.GetString(data, place, LastNameSize); place = place + LastNameSize; FirstNameSize = BitConverter.ToInt32(data, place); place += 4; FirstName = Encoding.ASCII.GetString(data, place, FirstNameSize); place += FirstNameSize; YearsService = BitConverter.ToInt32(data, place); place += 4; Salary = BitConverter.ToDouble(data, place); } public byte[] GetBytes() { byte[] data = new byte[1024]; int place = 0; Buffer.BlockCopy(BitConverter.GetBytes(EmployeeID), 0, data, place, 4); place += 4; Buffer.BlockCopy(BitConverter.GetBytes(LastName.Length), 0, data, place, 4); place += 4; Buffer.BlockCopy(Encoding.ASCII.GetBytes(LastName), 0, data, place, LastName.Length); place += LastName.Length; Buffer.BlockCopy(BitConverter.GetBytes(FirstName.Length), 0, data, place, 4); place += 4; Buffer.BlockCopy(Encoding.ASCII.GetBytes(FirstName), 0, data, place, FirstName.Length); place += FirstName.Length; Buffer.BlockCopy(BitConverter.GetBytes(YearsService), 0, data, place, 4); place += 4; Buffer.BlockCopy(BitConverter.GetBytes(Salary), 0, data, place, 8); place += 8; size = place; return data; } }
Var Tcp Server
/*
C# Network Programming
by Richard Blum
Publisher: Sybex
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class VarTcpSrvr
{
private static int SendVarData(Socket s, byte[] data)
{
int total = 0;
int size = data.Length;
int dataleft = size;
int sent;
byte[] datasize = new byte[4];
datasize = BitConverter.GetBytes(size);
sent = s.Send(datasize);
while (total < size) { sent = s.Send(data, total, dataleft, SocketFlags.None); total += sent; dataleft -= sent; } return total; } private static byte[] ReceiveVarData(Socket s) { int total = 0; int recv; byte[] datasize = new byte[4]; recv = s.Receive(datasize, 0, 4, 0); int size = BitConverter.ToInt32(datasize, 0); int dataleft = size; byte[] data = new byte[size]; while(total < size) { recv = s.Receive(data, total, dataleft, 0); if (recv == 0) { data = Encoding.ASCII.GetBytes("exit "); break; } total += recv; dataleft -= recv; } return data; } public static void Main() { byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Waiting for a client..."); Socket client = newsock.Accept(); IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Connected with {0} at port {1}", newclient.Address, newclient.Port); string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); int sent = SendVarData(client, data); for (int i = 0; i < 5; i++) { data = ReceiveVarData(client); Console.WriteLine(Encoding.ASCII.GetString(data)); } Console.WriteLine("Disconnected from {0}", newclient.Address); client.Close(); newsock.Close(); } } [/csharp]
Simple Tcp Server
/* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765 */ using System; using System.Net; using System.Net.Sockets; using System.Text; public class SimpleTcpSrvr { public static void Main() { int recv; byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Waiting for a client..."); Socket client = newsock.Accept(); IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port); string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); client.Send(data, data.Length, SocketFlags.None); while(true) { data = new byte[1024]; recv = client.Receive(data); if (recv == 0) break; Console.WriteLine( Encoding.ASCII.GetString(data, 0, recv)); client.Send(data, recv, SocketFlags.None); } Console.WriteLine("Disconnected from {0}", clientep.Address); client.Close(); newsock.Close(); } }
Stream Tcp Server
/* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765 */ using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; public class StreamTcpSrvr { public static void Main() { string data; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Waiting for a client..."); Socket client = newsock.Accept(); IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Connected with {0} at port {1}", newclient.Address, newclient.Port); NetworkStream ns = new NetworkStream(client); StreamReader sr = new StreamReader(ns); StreamWriter sw = new StreamWriter(ns); string welcome = "Welcome to my test server"; sw.WriteLine(welcome); sw.Flush(); while(true) { try { data = sr.ReadLine(); } catch (IOException) { break; } Console.WriteLine(data); sw.WriteLine(data); sw.Flush(); } Console.WriteLine("Disconnected from {0}", newclient.Address); sw.Close(); sr.Close(); ns.Close(); } }
Fixed Tcp Server
/*
C# Network Programming
by Richard Blum
Publisher: Sybex
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class FixedTcpSrvr
{
private static int SendData(Socket s, byte[] data)
{
int total = 0;
int size = data.Length;
int dataleft = size;
int sent;
while (total < size) { sent = s.Send(data, total, dataleft, SocketFlags.None); total += sent; dataleft -= sent; } return total; } private static byte[] ReceiveData(Socket s, int size) { int total = 0; int dataleft = size; byte[] data = new byte[size]; int recv; while(total < size) { recv = s.Receive(data, total, dataleft, 0); if (recv == 0) { data = Encoding.ASCII.GetBytes("exit "); break; } total += recv; dataleft -= recv; } return data; } public static void Main() { byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(ipep); newsock.Listen(10); Console.WriteLine("Waiting for a client..."); Socket client = newsock.Accept(); IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Connected with {0} at port {1}", newclient.Address, newclient.Port); string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); int sent = SendData(client, data); for (int i = 0; i < 5; i++) { data = ReceiveData(client, 9); Console.WriteLine(Encoding.ASCII.GetString(data)); } Console.WriteLine("Disconnected from {0}", newclient.Address); client.Close(); newsock.Close(); } } [/csharp]
Picky Tcp Client
/* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765 */ using System; using System.Net; using System.Net.Sockets; using System.Security; using System.Security.Permissions; using System.Text; [SocketPermission(SecurityAction.Deny, Access="Connect", Host="127.0.0.1", Port="All", Transport="All")] [SocketPermission(SecurityAction.Deny, Access="Connect", Host="192.168.0.2", Port="All", Transport="All")] [SocketPermission(SecurityAction.Deny, Access="Connect", Host="192.168.1.100", Port="80", Transport="All")] public class PickyTcpClient { public static void Main() { byte[] data = new byte[1024]; string input, stringData; TcpClient server = null; Console.Write("Enter a host to connect to: "); string stringHost = Console.ReadLine(); try { server = new TcpClient(stringHost, 9050); } catch (SocketException) { Console.WriteLine("Unable to connect to server"); return; } catch (SecurityException) { Console.WriteLine( "Sorry, you are restricted from connecting to this server"); return; } NetworkStream ns = server.GetStream(); int recv = ns.Read(data, 0, data.Length); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); while(true) { input = Console.ReadLine(); if (input == "exit") break; ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length); ns.Flush(); data = new byte[1024]; recv = ns.Read(data, 0, data.Length); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine(stringData); } Console.WriteLine("Disconnecting from server..."); ns.Close(); server.Close(); } }
Select Tcp Client
/* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765 */ using System; using System.Collections; using System.Net; using System.Net.Sockets; using System.Text; public class SelectTcpClient { public static void Main() { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); byte[] data = new byte[1024]; string stringData; int recv; sock.Connect(iep); Console.WriteLine("Connected to server"); recv = sock.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine("Received: {0}", stringData); while(true) { stringData = Console.ReadLine(); if (stringData == "exit") break; data = Encoding.ASCII.GetBytes(stringData); sock.Send(data, data.Length, SocketFlags.None); data = new byte[1024]; recv = sock.Receive(data); stringData = Encoding.ASCII.GetString(data, 0, recv); Console.WriteLine("Received: {0}", stringData); } sock.Close(); } }