基于Socket通信的BS结构文件服务器客户端的简易程序(1)
来源:互联网 发布:网页淘宝登录电脑版 编辑:程序博客网 时间:2024/05/21 17:27
最近因为课程需要,写了一个简单的BS的结构的,有客户端服务器的简易程序,但是整体效果不是很理想,不知道为什么在传送数据的过程中会出错,而且偶尔有异常报出。
整个软件的思想是:需要一个服务器,为每一个用户维护一个文件目录,用户可以上传和下载文件。再次基础上,我实现了显示在线用户的功能,显示在线用户的功能完成很好,几乎不会出错,用户上线和下线服务器都会通知其他的用户。
先给出客户端登录的简易代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;namespace Client{ public partial class Login : Form { IPAddress serverIP = null; private int server_port; TcpClient tcpClient = null; public Login() { InitializeComponent(); this.textBox_ip.Text = "10.108.13.27"; this.textBox_port.Text = "8888"; } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void button1_Click(object sender, EventArgs e) { // 检查用户出入的参数是否合法 string name = textBox1.Text.TrimEnd('\0') ; string password = textBox2.Text.TrimEnd('\0'); if (name == "" || password == ""||textBox_ip.Text==""||textBox_port.Text=="") { MessageBox.Show("错误的输入参数", "shit"); return; } else { // 用户输入的用户名和密码符合程序要求规范,和服务器进行通信,判断用户是否有权限登录系统 serverIP = IPAddress.Parse(textBox_ip.Text); server_port = int.Parse(textBox_port.Text); tcpClient = new TcpClient(); // 建立和服务器的连接 tcpClient.Connect(serverIP,server_port); if (tcpClient == null) { MessageBox.Show("无法连接到服务器,请重试!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { // 建立用于访问网络的基本数据流 NetworkStream netStream = tcpClient.GetStream(); byte[] name_byte = Encoding.Unicode.GetBytes(name); byte[] password_byte = Encoding.Unicode.GetBytes(password); // 分别给服务器发送用户名和密码 netStream.Write(name_byte,0,name_byte.Length); netStream.Flush(); byte[] test = new byte[2]; netStream.Read(test, 0, test.Length); netStream.Flush(); netStream.Write(password_byte,0,password_byte.Length); netStream.Flush(); // 接受服务器返回的登录反馈 byte[] echo_from_server = new byte[100]; netStream.Read(echo_from_server,0,echo_from_server.Length); string echo_string = Encoding.Unicode.GetString(echo_from_server).TrimEnd('\0'); // 用equals,不用==判断字符串 if (echo_string.Equals("failed")) { MessageBox.Show("用户名密码错误,请重新填写", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.textBox1.Text = ""; this.textBox2.Text = ""; this.tcpClient = null; return; } else if(echo_string.Equals("success")) { // 允许用户登录 MessageBox.Show("登录成功"); MainForm mainForm = new MainForm(name, netStream); mainForm.Owner = this; this.Hide(); mainForm.Show(); } } } } private void Login_Load(object sender, EventArgs e) { // //this.textBox2.PasswordChar = '*'; } }}
- 基于Socket通信的BS结构文件服务器客户端的简易程序(1)
- 基于Socket通信的BS结构文件服务器客户端的简易程序(2)
- 基于Socket通信的BS结构文件服务器客户端的简易程序(3)
- bs结构socket(udp)通信
- c语言gmail基于openssl的简易程序
- 基于tcp的socket通信(单个客户端与服务端)
- java实现基于TCP的socket通信实例(客户端)
- 用socket实现的文件服务器(1)
- 用socket实现的文件服务器(1)
- 用socket实现的文件服务器(1)
- 目前基于html的BS结构的缺陷
- 目前基于html的BS结构的缺陷(续)
- 【Java Socket】入门系列 3.2.编程实现基于 TCP 的 Socket 通信 — 服务器端和客户端
- 基于Socket通信的聊天室
- 基于UDP的socket通信
- 基于android的Socket通信
- 基于TCP的Socket通信
- 基于android的Socket通信
- errorlevel与%errorlevel%的区别
- VC2008以资源形式实现多语言版本(非Unicode)
- JTable的表头合并
- PowerDesigner 教程系列(三)概念数据模型
- ps材料 ps教程 精美按钮
- 基于Socket通信的BS结构文件服务器客户端的简易程序(1)
- Javascript PC Emulator
- DAVINCI DM365-DM368开发攻略——U-BOOT-2010.12及UBL的移植
- 怎样设计web“帮助页面”更有效 作者:Fred.ren 来源:DIV之家(转载请注明)
- C# 操作 word
- eclipse 中web app libraries 修改
- EL表达式中关于getParameter,getAttribute的区别
- C#中的Serializable
- Hive安装及问题解决