winsocket问题,在线求高手

yanxiaoyudotnet 2010-04-02 11:03:28
首先,本人这几年都是做B/S程序,对socket是小白级别,问的问题弱智也不要笑。公司有个小项目,就是从上位机获取数据,我把上位机的测试程序安装在本机测试。在cmd里键入netstat -an 得到的TCP的连接有以下几个:

TCP: 127.0.0.1:1049 0.0.0.0:0 LISTENING
TCP: 127.0.0.1:2069 127.0.0.1:30606 CLOSE_WAIT
TCP: 127.0.0.1:2169 127.0.0.1:30606 ESTABLISHED
TCP: 127.0.0.1:30606 0.0.0.0:0 LISTENING
TCP: 127.0.0.1:30606 127.0.0.1:2169 ESTABLISHED
TCP: 192.168.0.61:139 0.0.0.0:0 LISTENING
TCP: 192.168.0.61:2170 58.251.62.77:80 ESTABLISHED

一时不明白上位机服务的IP地址和端口号是多少?本地测试后,如果是远程的上位机IP地址肯定是不同,端口号会变吗?

下面是一个用户注册的申请协议!
“用户注册”申请
主题 序号 名称 长度 数值 备注
信息头 0 FA 1 &HFA 同步头
1 CmdNum 1 cLOGIN 命令号
2 Number 2 1 记录数量
4 PNum 1 20 每个记录内字节数量
数据部分 0 UserID 10 用户ID
10 UserPwd 10 用户密码
备注 业务终端 / 现场终端 -> 服务器

请问,我用TCPClient 连接之后,要发送到服务器的字符串(message)应该怎样拼写?

Int32 port = 30606;
TcpClient client = new TcpClient(server, port);

// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

// Get a client stream for reading and writing.
// Stream stream = client.GetStream();

NetworkStream stream = client.GetStream();

// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);



// Receive the TcpServer.response.

// Buffer to store the response bytes.
data = new Byte[256];

// String to store the response ASCII representation.
String responseData = String.Empty;

// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);



// Close everything.
stream.Close();
client.Close();

如果能有帮忙拼一下,或者有实例的程序就更好了,谢谢!
...全文
52 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanxiaoyudotnet 2010-04-02
  • 打赏
  • 举报
回复
如有高手愿意QQ帮助,就更加感谢了,QQ 85751330

1,978

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧