using System;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace tcpserver
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class server
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main( string [] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
int recv; // 用于表示客户端发送的信息长度
byte [] data;// = new byte [ 1024 ]; // 用于缓存客户端所发送的信息,通过socket传递的信息必须为字节数组
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050 ); // 本机预使用的IP和端口
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(); //当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信
IPEndPoint clientip = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine( " connect with client: " + clientip.Address + " at port: " + clientip.Port);
string welcome = " welcome here! " ;
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data,data.Length,SocketFlags.None); // 发送信息
while ( true )
{ // 用死循环来不断的从客户端获取信息
data = new byte [ 1024 ];
recv = client.Receive(data);
Console.WriteLine( " recv= " + recv);
if (recv == 0 ) // 当信息长度为0,说明客户端连接断开
break ;
Console.WriteLine(Encoding.ASCII.GetString(data, 0 ,recv));
client.Send(data,recv,SocketFlags.None);
}
Console.WriteLine( " Disconnected from " + clientip.Address);
client.Close();
newsock.Close();
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace tcpclient
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class client
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
byte[] data = new byte[1024];
Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newclient.Bind(new IPEndPoint(IPAddress.Any, 905));
Console.Write(" please input the server ip: ");
string ipadd = Console.ReadLine();
Console.WriteLine();
Console.Write(" please input the server port: ");
int port = Convert.ToInt32(Console.ReadLine());
IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port); // 服务器的IP和端口
try
{
// 因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。
newclient.Connect(ie);
}
catch (SocketException e)
{
Console.WriteLine(" unable to connect to server ");
Console.WriteLine(e.ToString());
return;
}
int receivedDataLength = newclient.Receive(data);
string stringdata = Encoding.ASCII.GetString(data, 0, receivedDataLength);
Console.WriteLine(stringdata);
while (true)
{
string input = Console.ReadLine();
if (input == " exit ")
break;
newclient.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
receivedDataLength = newclient.Receive(data);
stringdata = Encoding.ASCII.GetString(data, 0, receivedDataLength);
Console.WriteLine(stringdata);
}
Console.WriteLine(" disconnect from sercer ");
newclient.Shutdown(SocketShutdown.Both);
newclient.Close();
}
}
}
while (true) // 用死循环来不断的获取信息
{
data = new byte[1024];
recv = newclient.Receive(data);
uiContext.Send(new SendOrPostCallback(
state =>
{
int txtGetMsgLength = txtGetMsg.Text.Length;
string recMsg = "Friend: " + System.DateTime.Now.ToString() + "\n " +Encoding.Unicode.GetString(data, 0, recv) + "\n";
txtGetMsg.AppendText(recMsg);
txtGetMsg.Select(txtGetMsgLength, recMsg.Length - Encoding.Unicode.GetString(data, 0, recv).Length - 1);
txtGetMsg.SelectionColor = Color.Red;
}), null);
}
private void btnSendMsg_Click(object sender, EventArgs e)
{
string input = txtSendMsg.Text;
if (input == "")
{
MessageBox.Show("消息不能为空!", "发送消息出错");
txtSendMsg.Focus();
}
else
{
if (meIsClient)
{
newclient.Send(Encoding.Unicode.GetBytes(input));
string showText = "Me: " + System.DateTime.Now.ToString() + "\n "
+ input + "\n";
int txtGetMsgLength = txtGetMsg.Text.Length;
txtGetMsg.AppendText(showText);
txtGetMsg.Select(txtGetMsgLength, showText.Length - 1 - input.Length);
txtGetMsg.SelectionColor = Color.Blue;
txtSendMsg.Text = "";
}
else
{
client.Send(Encoding.Unicode.GetBytes(input));
string showText = "Me " + System.DateTime.Now.ToString() + "\n "
+ input + "\n";
int txtGetMsgLength = txtGetMsg.Text.Length;
txtGetMsg.AppendText(showText);
txtGetMsg.Select(txtGetMsgLength, showText.Length - 1 - input.Length);
txtGetMsg.SelectionColor = Color.Blue;
txtSendMsg.Text = "";
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有