Recently, because of the need of the course , Wrote a simple BS Structural , Simple program with client server , But the overall effect is not very good , I don't know why something went wrong during the data transfer , And there are occasional abnormal reports .
The idea of the whole software is : A server is required , Maintain a file directory for each user , Users can upload and download files . Again on the basis of , I realized the function of displaying online users , The function of displaying online users is very good , Almost no mistakes , When a user goes online or offline, the server will notify other users .
First give the simple code of client login :
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) { // Check whether the user's access parameters are legal string name = textBox1.Text.TrimEnd('\0') ;
string password = textBox2.Text.TrimEnd('\0'); if (name == "" || password ==
""||textBox_ip.Text==""||textBox_port.Text=="") { MessageBox.Show(" Wrong input parameter ",
"shit"); return; } else { // The user name and password entered by the user meet the specification of program requirements , Communicate with server , Judge whether the user has the right to log in to the system
serverIP = IPAddress.Parse(textBox_ip.Text); server_port =
int.Parse(textBox_port.Text); tcpClient = new TcpClient(); // Establish a connection to the server
tcpClient.Connect(serverIP,server_port); if (tcpClient == null) {
MessageBox.Show(" Unable to connect to the server , Please try again !", " error ", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation); } else { // Establish the basic data flow for accessing the network NetworkStream netStream
= tcpClient.GetStream(); byte[] name_byte = Encoding.Unicode.GetBytes(name);
byte[] password_byte = Encoding.Unicode.GetBytes(password); // Send the user name and password to the server respectively
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(); //
Accept the login feedback from the server 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'); //
use equals, no need == Judgment string if (echo_string.Equals("failed")) {
MessageBox.Show(" Wrong user name and password , Please fill in again ", " Tips ", MessageBoxButtons.OK,
MessageBoxIcon.Information); this.textBox1.Text = ""; this.textBox2.Text = "";
this.tcpClient = null; return; } else if(echo_string.Equals("success")) { //
Allow users to log in MessageBox.Show(" Login successful "); MainForm mainForm = new MainForm(name,
netStream); mainForm.Owner = this; this.Hide(); mainForm.Show(); } } } }
private void Login_Load(object sender, EventArgs e) { //
//this.textBox2.PasswordChar = '*'; } } }
Technology