string ip = "***.**.**.**";
int port = 9001;
IPEndPoint iep = new IPEndPoint(IPAddress.Parse(ip), port);
Socket socketsmg = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketsmg.Bind(iep);
socketsmg.Listen(100);
ThreadDeal deal = new ThreadDeal(socketsmg);
Thread thread = new Thread(new ThreadStart(deal.Deal));
thread.IsBackground = true;
thread.Start();
2.sp侦听线程类
public class ThreadDeal
{
Socket server = null;
Socket client = null;
public ThreadDeal(Socket s)
{
server = s;
}
public void Deal()
{
while (true)
{
client = server.Accept();
if (client != null)
{
ThreadRece threadRec = new ThreadRece(client);
Thread threadRece = new Thread(new ThreadStart(threadRec.ThreadReceDeal));//在新的线程中接收数据
threadRece.IsBackground = true;
threadRece.Start();
}
}
}
}
3.接收消息和应答消息线程类
public class ThreadRece
{
Socket client;
public ThreadRece(Socket input)
{
client = input;
}
public void ThreadReceDeal()
{
IPEndPoint iep = client.RemoteEndPoint as IPEndPoint;
MessageBox.Show("客户端套接字:" + iep.Address.ToString() + ":" +iep.Port.ToString());
byte[] bindbyte = new byte[61];
int receiveBindCount = client.Receive(bindbyte);
byte[] seq = Util.GetSeq(bindbyte);
try
{
byte[] byteReturn = new byte[29];
byte[] len = BitConvert.uint2Bytes(29);
byte[] cmdid = BitConvert.uint2Bytes(0x80000001);
len.CopyTo(byteReturn, 0);
cmdid.CopyTo(byteReturn, 4);
seq.CopyTo(byteReturn, 8);
int returnCount = 0;
if (client.Connected == true)
{
returnCount = client.Send(byteReturn, 0, 29, SocketFlags.None);
}
else
{
MessageBox.Show("Client连接已经关闭");
}
MessageBox.Show("发送的字节数量" + returnCount.ToString());
client.Close();
client = null;
}
catch (Exception e)
{
MessageBox.Show("转换错误" + e.ToString());
}
}
}
情况
1。是远程客户端对我是不可见的,不能控制。远程客户端可能没有问题,或者设置上有问题,但是我都不确定。
2.我自己写了客户端来连接服务器,我的客户端是可以接收到服务器的应答信息的。但是那个远程客户端就不行。
3.这个远程客户端是联通的服务器一般来说不会有问题,或者就是配置有问题。
4.请大家先看看我的代码逻辑,可能的问题还是我自己的代码问题。
6 个解决方案
#1
Thread t_linsten = new Thread(jf_socket.BeginListen);
jf_socket.PRForm = this;
t_linsten.Start();
//开始监听
public void BeginListen()
{
IPAddress ServerIp = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint iep = new IPEndPoint(ServerIp, 8000);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] byteMessage = new byte[100];
socket.Bind(iep);
while (true)
{
try
{
socket.Listen(10);
Socket newSocket = socket.Accept();
newSocket.Receive(byteMessage);
Jf_Opereration oper = new Jf_Opereration();
oper.Tr_Serverinfo(Encoding.Default.GetString(byteMessage),out re_message,out re_manageinfo);
//跨线程消息处理
MethodInvoker mi = new MethodInvoker(FormShow);
prform.BeginInvoke(mi);
}
catch (SocketException ex)
{
File.AppendAllText(@"log.txt",DateTime.Now.ToString() + ": " + ex.ToString() + " \n");
socket.Close();
}
}
}
public String Re_Message
{
get { return re_message; }
}
//主窗体
public Form PRForm
{
get {
return prform;
}
set {
prform = value;
}
}
//发送消息
public bool BeginSend(IPAddress serverIp, Int32 port, string msg,out string out_err)
{
IPEndPoint iep = new IPEndPoint(serverIp, 8000);
byte[] byteMessage = Encoding.Default.GetBytes(msg);
Socket send_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
out_err = null;
try
{
send_socket.Connect(iep);
send_socket.Send(byteMessage);
return true;
}
catch (SocketException ex)
{
out_err = ex.ToString();
return false;
}
finally
{
send_socket.Close();
}
}
修改 删除 举报 引用 回复
修改 删除 举报 引用 回复
加为好友
发送私信
在线聊天
sunchaohuang
流星飞狐(oO!)
等级:
可用分等级:富农
总技术分:4754
总技术分排名:4537
发表于:2008-12-15 16:03:108楼 得分:0
C# codeString re_message, re_manageinfo; //服务器返回信息和管理员信息
Form prform;//主窗体
public bool Tr_Serverinfo(string message,out string re_msg,out string re_manageinfo)
{
}
修改 删除 举报 引用 回复
加为好友
发送私信
在线聊天
sunchaohuang
流星飞狐(oO!)
等级:
可用分等级:富农
总技术分:4754
总技术分排名:4537
发表于:2008-12-15 16:08:049楼 得分:0
以上代码服务端放一个 客户端放一个 然后就OK了 调试通过的
看看这你能不能用
#2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace Server
{
public partial class Form1 : Form
{
private TcpListener TL = null;
private IPEndPoint qSerIpendPoint;
private Thread qSerTh;
public Form1()
{
InitializeComponent();
}
private void Start()
{
try
{
int IpPort = 1000;
string Ip = "127.0.0.1";
IPAddress Address = IPAddress.Parse(Ip);
qSerIpendPoint = new IPEndPoint(Address, IpPort);
TL = new TcpListener(qSerIpendPoint);
TL.Start();
qSerTh = new Thread(new ThreadStart(Receive));
qSerTh.Start();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Receive()
{
try
{
Socket socket;
socket = TL.AcceptSocket();
string RemoteEndPoint = socket.RemoteEndPoint.ToString();
Byte[] Stream = new Byte[1024];
socket.Receive(Stream);//接收传来的值
UTF8Encoding ServerReceiveStr = new UTF8Encoding();
socket.Send(ServerReceiveStr.GetBytes("Ok,Thank You"));//向客服端发送一个回复信息
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
Start();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Timers;
namespace Client
{
public partial class Form1 : Form
{
private System.Timers.Timer _timer;
private int _Interval=30000;
public Form1()
{
InitializeComponent();
_timer = new System.Timers.Timer();
_timer.Enabled = true;
_timer.Interval = _Interval;
_timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Connect();
}
private void Connect()
{
try
{
string ServerIp = "127.0.0.1";//服务器地址
int ServerPort = 1000;//服务器端口号
TcpClient TC = new TcpClient();
TC.Connect(ServerIp, ServerPort);//建立与服务器的连接
Stream stm = TC.GetStream();//通过tcp
UTF8Encoding UTF8 = new UTF8Encoding();
byte[] str = UTF8.GetBytes("maomao");
stm.Write(str, 0, str.Length);//向服务器发送信息
byte[] bb = new byte[1024];
int k = stm.Read(bb, 0, 1024);
string RevStr = string.Empty;
RevStr = System.Text.UTF8Encoding.UTF8.GetString(bb,0,k);
MessageBox.Show(RevStr);
stm.Close();
TC.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
Connect();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace Server
{
public partial class Form1 : Form
{
private TcpListener TL = null;
private IPEndPoint qSerIpendPoint;
private Thread qSerTh;
public Form1()
{
InitializeComponent();
}
private void Start()
{
try
{
int IpPort = 1000;
string Ip = "127.0.0.1";
IPAddress Address = IPAddress.Parse(Ip);
qSerIpendPoint = new IPEndPoint(Address, IpPort);
TL = new TcpListener(qSerIpendPoint);
TL.Start();
qSerTh = new Thread(new ThreadStart(Receive));
qSerTh.Start();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Receive()
{
try
{
Socket socket;
socket = TL.AcceptSocket();
string RemoteEndPoint = socket.RemoteEndPoint.ToString();
Byte[] Stream = new Byte[1024];
socket.Receive(Stream);//接收传来的值
UTF8Encoding ServerReceiveStr = new UTF8Encoding();
socket.Send(ServerReceiveStr.GetBytes("Ok,Thank You"));//向客服端发送一个回复信息
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
Start();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Timers;
namespace Client
{
public partial class Form1 : Form
{
private System.Timers.Timer _timer;
private int _Interval=30000;
public Form1()
{
InitializeComponent();
_timer = new System.Timers.Timer();
_timer.Enabled = true;
_timer.Interval = _Interval;
_timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Connect();
}
private void Connect()
{
try
{
string ServerIp = "127.0.0.1";//服务器地址
int ServerPort = 1000;//服务器端口号
TcpClient TC = new TcpClient();
TC.Connect(ServerIp, ServerPort);//建立与服务器的连接
Stream stm = TC.GetStream();//通过tcp
UTF8Encoding UTF8 = new UTF8Encoding();
byte[] str = UTF8.GetBytes("maomao");
stm.Write(str, 0, str.Length);//向服务器发送信息
byte[] bb = new byte[1024];
int k = stm.Read(bb, 0, 1024);
string RevStr = string.Empty;
RevStr = System.Text.UTF8Encoding.UTF8.GetString(bb,0,k);
MessageBox.Show(RevStr);
stm.Close();
TC.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
Connect();
}
}
}
#3
我是这样写得
不知道对你是否有帮助
不知道对你是否有帮助
#4
是不是发送的时候少了这个:
send_socket.Connect(iep);
send_socket.Connect(iep);
#5
客户端收不到应答,是不是可以理解你已经收到了客户端的信息?
如果是的话,你是否是调试一下看下面这句有什么错误。。。
returnCount = client.Send(byteReturn, 0, 29, SocketFlags.None);
如果是的话,你是否是调试一下看下面这句有什么错误。。。
returnCount = client.Send(byteReturn, 0, 29, SocketFlags.None);
#6
returnCount = client.Send(byteReturn, 0, 29, SocketFlags.None);
我这句话没有错误的。我弹出returnCount 这个变量不为空的
我这句话没有错误的。我弹出returnCount 这个变量不为空的
#1
Thread t_linsten = new Thread(jf_socket.BeginListen);
jf_socket.PRForm = this;
t_linsten.Start();
//开始监听
public void BeginListen()
{
IPAddress ServerIp = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint iep = new IPEndPoint(ServerIp, 8000);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] byteMessage = new byte[100];
socket.Bind(iep);
while (true)
{
try
{
socket.Listen(10);
Socket newSocket = socket.Accept();
newSocket.Receive(byteMessage);
Jf_Opereration oper = new Jf_Opereration();
oper.Tr_Serverinfo(Encoding.Default.GetString(byteMessage),out re_message,out re_manageinfo);
//跨线程消息处理
MethodInvoker mi = new MethodInvoker(FormShow);
prform.BeginInvoke(mi);
}
catch (SocketException ex)
{
File.AppendAllText(@"log.txt",DateTime.Now.ToString() + ": " + ex.ToString() + " \n");
socket.Close();
}
}
}
public String Re_Message
{
get { return re_message; }
}
//主窗体
public Form PRForm
{
get {
return prform;
}
set {
prform = value;
}
}
//发送消息
public bool BeginSend(IPAddress serverIp, Int32 port, string msg,out string out_err)
{
IPEndPoint iep = new IPEndPoint(serverIp, 8000);
byte[] byteMessage = Encoding.Default.GetBytes(msg);
Socket send_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
out_err = null;
try
{
send_socket.Connect(iep);
send_socket.Send(byteMessage);
return true;
}
catch (SocketException ex)
{
out_err = ex.ToString();
return false;
}
finally
{
send_socket.Close();
}
}
修改 删除 举报 引用 回复
修改 删除 举报 引用 回复
加为好友
发送私信
在线聊天
sunchaohuang
流星飞狐(oO!)
等级:
可用分等级:富农
总技术分:4754
总技术分排名:4537
发表于:2008-12-15 16:03:108楼 得分:0
C# codeString re_message, re_manageinfo; //服务器返回信息和管理员信息
Form prform;//主窗体
public bool Tr_Serverinfo(string message,out string re_msg,out string re_manageinfo)
{
}
修改 删除 举报 引用 回复
加为好友
发送私信
在线聊天
sunchaohuang
流星飞狐(oO!)
等级:
可用分等级:富农
总技术分:4754
总技术分排名:4537
发表于:2008-12-15 16:08:049楼 得分:0
以上代码服务端放一个 客户端放一个 然后就OK了 调试通过的
看看这你能不能用
#2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace Server
{
public partial class Form1 : Form
{
private TcpListener TL = null;
private IPEndPoint qSerIpendPoint;
private Thread qSerTh;
public Form1()
{
InitializeComponent();
}
private void Start()
{
try
{
int IpPort = 1000;
string Ip = "127.0.0.1";
IPAddress Address = IPAddress.Parse(Ip);
qSerIpendPoint = new IPEndPoint(Address, IpPort);
TL = new TcpListener(qSerIpendPoint);
TL.Start();
qSerTh = new Thread(new ThreadStart(Receive));
qSerTh.Start();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Receive()
{
try
{
Socket socket;
socket = TL.AcceptSocket();
string RemoteEndPoint = socket.RemoteEndPoint.ToString();
Byte[] Stream = new Byte[1024];
socket.Receive(Stream);//接收传来的值
UTF8Encoding ServerReceiveStr = new UTF8Encoding();
socket.Send(ServerReceiveStr.GetBytes("Ok,Thank You"));//向客服端发送一个回复信息
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
Start();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Timers;
namespace Client
{
public partial class Form1 : Form
{
private System.Timers.Timer _timer;
private int _Interval=30000;
public Form1()
{
InitializeComponent();
_timer = new System.Timers.Timer();
_timer.Enabled = true;
_timer.Interval = _Interval;
_timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Connect();
}
private void Connect()
{
try
{
string ServerIp = "127.0.0.1";//服务器地址
int ServerPort = 1000;//服务器端口号
TcpClient TC = new TcpClient();
TC.Connect(ServerIp, ServerPort);//建立与服务器的连接
Stream stm = TC.GetStream();//通过tcp
UTF8Encoding UTF8 = new UTF8Encoding();
byte[] str = UTF8.GetBytes("maomao");
stm.Write(str, 0, str.Length);//向服务器发送信息
byte[] bb = new byte[1024];
int k = stm.Read(bb, 0, 1024);
string RevStr = string.Empty;
RevStr = System.Text.UTF8Encoding.UTF8.GetString(bb,0,k);
MessageBox.Show(RevStr);
stm.Close();
TC.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
Connect();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace Server
{
public partial class Form1 : Form
{
private TcpListener TL = null;
private IPEndPoint qSerIpendPoint;
private Thread qSerTh;
public Form1()
{
InitializeComponent();
}
private void Start()
{
try
{
int IpPort = 1000;
string Ip = "127.0.0.1";
IPAddress Address = IPAddress.Parse(Ip);
qSerIpendPoint = new IPEndPoint(Address, IpPort);
TL = new TcpListener(qSerIpendPoint);
TL.Start();
qSerTh = new Thread(new ThreadStart(Receive));
qSerTh.Start();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Receive()
{
try
{
Socket socket;
socket = TL.AcceptSocket();
string RemoteEndPoint = socket.RemoteEndPoint.ToString();
Byte[] Stream = new Byte[1024];
socket.Receive(Stream);//接收传来的值
UTF8Encoding ServerReceiveStr = new UTF8Encoding();
socket.Send(ServerReceiveStr.GetBytes("Ok,Thank You"));//向客服端发送一个回复信息
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
Start();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Timers;
namespace Client
{
public partial class Form1 : Form
{
private System.Timers.Timer _timer;
private int _Interval=30000;
public Form1()
{
InitializeComponent();
_timer = new System.Timers.Timer();
_timer.Enabled = true;
_timer.Interval = _Interval;
_timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Connect();
}
private void Connect()
{
try
{
string ServerIp = "127.0.0.1";//服务器地址
int ServerPort = 1000;//服务器端口号
TcpClient TC = new TcpClient();
TC.Connect(ServerIp, ServerPort);//建立与服务器的连接
Stream stm = TC.GetStream();//通过tcp
UTF8Encoding UTF8 = new UTF8Encoding();
byte[] str = UTF8.GetBytes("maomao");
stm.Write(str, 0, str.Length);//向服务器发送信息
byte[] bb = new byte[1024];
int k = stm.Read(bb, 0, 1024);
string RevStr = string.Empty;
RevStr = System.Text.UTF8Encoding.UTF8.GetString(bb,0,k);
MessageBox.Show(RevStr);
stm.Close();
TC.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
Connect();
}
}
}
#3
我是这样写得
不知道对你是否有帮助
不知道对你是否有帮助
#4
是不是发送的时候少了这个:
send_socket.Connect(iep);
send_socket.Connect(iep);
#5
客户端收不到应答,是不是可以理解你已经收到了客户端的信息?
如果是的话,你是否是调试一下看下面这句有什么错误。。。
returnCount = client.Send(byteReturn, 0, 29, SocketFlags.None);
如果是的话,你是否是调试一下看下面这句有什么错误。。。
returnCount = client.Send(byteReturn, 0, 29, SocketFlags.None);
#6
returnCount = client.Send(byteReturn, 0, 29, SocketFlags.None);
我这句话没有错误的。我弹出returnCount 这个变量不为空的
我这句话没有错误的。我弹出returnCount 这个变量不为空的