public void send(){ //发送
string msg="你好"
string ip = "192.168.1.5"; //这是我寝室好友的IP
socketReceive = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ipSent = new IPEndPoint(IPAddress.Parse(ip), 8001);
socketSent.Connect(ipSent);
socketSent.Send(Encoding.Default.GetBytes(msg));
socketSent.Close();
} private void ReceiveNews() //接收
{
try
{
socketReceive = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);在运行这句之后,不再向下运行了,(我是在窗体运行的时候启用线程调用的)
ipReceive = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0],8001);
socketReceive.Bind(ipReceive);
socketReceive.Listen(1024);
while (true)
{
Socket chat = socketReceive.Accept();
IPEndPoint ep = (IPEndPoint)chat.RemoteEndPoint;
byte[] buff = new byte[1024];
int len;
while ((len = chat.Receive(buff)) != 0)
{
string msg = Encoding.Default.GetString(buff, 0, len);
pack = msg.Substring(0, 1);
string cmd = msg.Substring(1, 3);
msg = Encoding.Default.GetString(buff);
textBox2.Text = msg;
}
}
}
}我调试很长时间了在网上看了一点通信的,第一次写。见笑了。请大家帮一下了。
string msg="你好"
string ip = "192.168.1.5"; //这是我寝室好友的IP
socketReceive = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ipSent = new IPEndPoint(IPAddress.Parse(ip), 8001);
socketSent.Connect(ipSent);
socketSent.Send(Encoding.Default.GetBytes(msg));
socketSent.Close();
} private void ReceiveNews() //接收
{
try
{
socketReceive = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);在运行这句之后,不再向下运行了,(我是在窗体运行的时候启用线程调用的)
ipReceive = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0],8001);
socketReceive.Bind(ipReceive);
socketReceive.Listen(1024);
while (true)
{
Socket chat = socketReceive.Accept();
IPEndPoint ep = (IPEndPoint)chat.RemoteEndPoint;
byte[] buff = new byte[1024];
int len;
while ((len = chat.Receive(buff)) != 0)
{
string msg = Encoding.Default.GetString(buff, 0, len);
pack = msg.Substring(0, 1);
string cmd = msg.Substring(1, 3);
msg = Encoding.Default.GetString(buff);
textBox2.Text = msg;
}
}
}
}我调试很长时间了在网上看了一点通信的,第一次写。见笑了。请大家帮一下了。
解决方案 »
- 请教线程的同步
- TextBox控件如何設置得到焦點時是全選狀態。
- ADOX.Catalog创建Access文件报错 没有注册类
- 自定义控件重写Font属性时怎么会出错??
- 怎样让模板列中的textbox居右显示?
- 非常急。。。。。。。。。。。。。。。。。
- 如何将一个Html格式文件转换为Excel文件(可以用OleDb读取的Excel格式)。
- 异步socket通讯问题
- 怎么让一个控制台应用程序在后台运行啊
- 如何让TextBox输入的数值 始终显示为货币格式 如:2321.00、222.20
- c#(csharp)中invoke的问题,希望各位能够指点一下
- datatable中取值方式问:linq 性能问题
server.cs
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;public class server
{
public static void Main()
{
try
{
// 把IP地址转换为IPAddress的实例
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
// 初始化监听器, 端口为8001
TcpListener myList=new TcpListener(ipAd,8001);
// 开始监听服务器端口
myList.Start(); // 输出服务器启动信息
Console.WriteLine("在8001端口启动服务...");
Console.WriteLine("本地节点为:" + myList.LocalEndpoint );
Console.WriteLine("等待连接.....");
// 等待处理接入连接请求
// 新建立的连接用套接字s表示
Socket s=myList.AcceptSocket();
Console.WriteLine("连接来自 "+s.RemoteEndPoint);
// 接收客户端信息
byte[] b=new byte[100];
int k=s.Receive(b);
Console.WriteLine("已接收...");
for (int i=0;i<k;i++)
{
Console.Write(Convert.ToChar(b[i]));
}
// 处理客户端请求,给客户端回应
ASCIIEncoding asen=new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\n已发送回应信息");
// 善后工作,释放资源
s.Close();
myList.Stop();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}client.cs
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;public class client
{
public static void Main()
{
try
{
// 新建客户端套接字
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("连接.....");
// 连接服务器
tcpclnt.Connect("127.0.0.1",8001);
Console.WriteLine("已连接");
Console.Write("请输入要传输的字符串 : ");
// 读入字符串
String str=Console.ReadLine(); // 得到客户端的流
Stream stm = tcpclnt.GetStream();
// 发送字符串
ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes(str);
Console.WriteLine("传输中.....");
stm.Write(ba,0,ba.Length);
// 接收从服务器返回的信息
byte[] bb=new byte[100];
int k=stm.Read(bb,0,100);
// 输出服务器返回信息
for (int i=0;i<k;i++)
{
Console.Write(Convert.ToChar(bb[i]));
}
// 关闭客户端连接
tcpclnt.Close();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
public Socket socketReceive = null;
//定义发送信息的IP地址和端口号
public IPEndPoint ipSent = null;
//定义作为客户端发送信息套接字
public Socket socketSent = null;
//定义接受信息的IP地址和端口号
public IPEndPoint ipReceive = null;
这个在类的开始的那里建立的