using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
IPAddress ipAd = IPAddress.Parse("127.0.0.2");
TcpListener myList = new TcpListener(ipAd, 8001);
myList.Start();
Console.WriteLine("my local address:" + myList.LocalEndpoint);
Console.WriteLine("wait");
Socket s = myList.AcceptSocket();
Console.WriteLine("remote address: " + s.RemoteEndPoint);
int j = 0;
while(j!=10)
{
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("received");
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(b[i]));
}
Console.WriteLine();
Console.WriteLine("replying");
string str = Console.ReadLine();
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(str));
}
}
catch { }
Console.ReadLine();
}
}
}
--------------------------------------------------------------------------------------
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("connecting to 127.0.0.2,8001");
tcpclnt.Connect("127.0.0.2",8001);
Console.WriteLine("connected");
int j = 0;
while (j!=10)
{
Console.WriteLine();
Console.WriteLine("sending");
string str = Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
Console.WriteLine("receiving");
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(bb[i]));
}
j++;
}
Console.ReadLine();
tcpclnt.Close();
}
catch(Exception e)
{
Console.WriteLine("error"+e.StackTrace);
}
}
}
}
No comments:
Post a Comment