请帮助,C#客户端服务器编程中的问题 [英] please help, problem in client server programming in c#

查看:49
本文介绍了请帮助,C#客户端服务器编程中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c#创建客户端服务器程序
客户端程序是表单应用程序,服务器是控制台应用程序.

在客户端中,我有4种形式,当我从第一种形式发送数据时,服务器可以接收它,但是当我从另一种形式发送时,服务器无法接收数据.
有人知道为什么吗?

外观:在客户端,我使用了Windows应用程序形式,

第一个表单将使用用户名和密码并将其发送到服务器.

然后服务器检查此用户是否可用,并将可用性发送给
客户,

然后客户打开另一个表单并关闭第一个表单

并询问一些详细信息,例如tel,mob等,(直到这里我没有
任何问题)

那么它应该将详细信息发送到服务器,

但是服务器似乎不接受其他形式的数据,似乎只连接到第一种形式.

form1的代码是:

i create client server program using c#
client program is form application and server is console application.

in client i had 4 forms, when i send data from first form , server can receive it, but when i send from another form , server can not receive the data.
any one know why??

look : in client side i used windows application form,

first form it takes user name and password and send it to server.

then server check if this user is available and send availability to
client,

then client open another form and close the first form

and ask for some details like tel,mob and etc,(until here i don''t have
any problem)

then it should send details to server,

but it seems the server dosen''t accept the data from another form, it seems it connect only to first form.

the code for form1 is :

TcpClient c = new TcpClient("127.0.0.1", 1000);
NetworkStream ns = c.GetStream();
string st1 = textBox1.Text;
string st2 = textBox2.Text;
string name = string.Concat("F" + len + len2 + len3 + st1 + passhash + st3);
byte[] buf = System.Text.Encoding.ASCII.GetBytes(name);  // convert string to an array of bytes
ns.Write(buf, 0, name.Length);  // write to stream




此方法适用于第一种形式,但是当我将此代码用于另一种形式不起作用时,我不知道为什么吗?

现在我知道了问题,但我不知道如何解决
似乎每种形式都需要特定的端口号.
在这种情况下,我无法为每种形式分配端口,因为它具有多种形式,另一方面,在服务器端,我应该侦听许多端口.

我尝试制作子窗体,但它也无法正常工作,并且它不继承tcpclient.

那么,解决方案是什么?

这是服务器代码:




this work for first form,but when i use this code for another form it doesn''t work, i don''t no why?

now i know the problem but i don''t know how to solve this
it seems each form needs specific port number.
in this case i can not assign to each form a port, because it had many form, in other hand in server side i should listen to many port.

i tried to make child form, but it also doesn''t work, and it''s not inherit tcpclient.

so, what would the solution be ?

this is server code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWMOdel;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener l = new TcpListener(IPAddress.Any, 1000);

            l.Start();
            Console.WriteLine("Server has started.Waiting for clients...");
            TcpClient c = l.AcceptTcpClient();
            Console.WriteLine(" >> Accept Connection From Clinet ");
            NetworkStream ns = c.GetStream();

           while(true)
           {
               byte[] buf = new byte[200];
                ns.Read(buf, 0, 200);
                string st = System.Text.Encoding.ASCII.GetString(buf);
                Console.WriteLine(st);


                //devide the string
                int le = st[1];
                int le2 = st[2];
                int le3 = st[3];
                string flag = st.Substring(0, 1);

                string name;
                string pass;
                string position;
                name = st.Substring(4, le);
                pass = st.Substring(4 + le, le2);
                position = st.Substring(4 + le + le2, le3);
                
                Console.WriteLine(name);
                
               //finish divede string

               byte[] buf2;
                if (string.Compare(name, "Administrator")==0)
                {
                    string one = Convert.ToString(1);
                    buf2 = System.Text.Encoding.ASCII.GetBytes(Convert.ToString(one));
                    ns.Write(buf2, 0, one.Length);
                }
              
                NetworkStream ns2 = c.GetStream();
                byte[] stin = new byte[200];
                ns.Read(stin, 0, 100);
                string stinfo = System.Text.Encoding.ASCII.GetString(stin);
                Console.WriteLine(stinfo);
                

            }//end while true



        }//end of main
       
    }//end of class program
}//end of namespace




这是客户代码:
表格1:




this is client code :
form 1 :

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;
using System.Security.Cryptography;

namespace newform1
{
    

    public partial class Form1 : Form
    {

    

        TcpClient c = new TcpClient("127.0.0.1", 1000);

        public Form1()
        {
            InitializeComponent();
            textBox2.PasswordChar = '*';
        }

        private void Form1_Load(object sender, EventArgs e)
        {
             NetworkStream ns = c.GetStream();
          
          

        }

        public string CreateMD5Hash(string input)
        {
            // Use input string to calculate MD5 hash
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);

            // Convert the byte array to hexadecimal string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("x2"));

            }
            return sb.ToString();
        }//end of create md5

        private void button1_Click(object sender, EventArgs e)
        {
          NetworkStream ns = c.GetStream();

            string st1 = textBox1.Text;
            string st2 = textBox2.Text;
            string st3 = comboBox1.Text;
           string passhash = CreateMD5Hash(st2);
            int lenght1 = st1.Length;
            int lenght2 = st2.Length;
            int length3 = st3.Length;


            char len;
            char len2;
            char len3;
            len = Convert.ToChar(lenght1);
            len2 = Convert.ToChar(lenght2);
            len3 = Convert.ToChar(length3);

            string name = string.Concat("F" + len + len2 + len3 + st1 + passhash + st3);
           
           byte[] buf = System.Text.Encoding.ASCII.GetBytes(name);  // convert string to an array of bytes
        ns.Write(buf, 0, name.Length);  // write to stream

          // here it accept permission from server to open new form
           byte[] buf2 = new byte[100];
       ns.Read(buf2, 0, 100);

           string add = System.Text.Encoding.ASCII.GetString(buf2);
           if(string.Compare(add,"1")==1)
            {
                this.Hide();
                studentform myform = new studentform();
                myform.Show();
            }
          
        }//end of button 1 click
        
    }//end of class form
}//end of namespac



这是第二种形式的代码:



and this is code for second form :

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 newform1
{
    public partial class Form2 : Form
    {
        TcpClient c = new TcpClient("127.0.0.1", 1000); 
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NetworkStream ns = c.GetStream();
           string stin = "1";
            byte[] buf = System.Text.Encoding.ASCII.GetBytes(stin);  // convert string to an array of bytes
           ns.Write(buf, 0, stin.Length);  // write to stream
            addremst myform = new addremst();
            this.Hide();
            myform.Show();
        }
    }
}



正如我在将一个端口分配给两种形式之前所说的那样,似乎服务器不接受第二种形式的连接,我希望它们使用相同的端口,
我有很多形式,我不想给每个端口.



as i told before i gave one port to two form, it seems server doesn''t accept connection from second form, i want them to use same port,
i have to many form, i don''t want to give each one , port.

推荐答案

不可能不看一些就猜出你的问题是什么客户端和服务器的更多代码.您确定您的第一个表单在完成连接后将关闭它吗?您还声明不确定服务器是处于打开状态还是在监听状态,所以您如何期望它正常工作?
It''s impossible to guess what your problem is without seeing some more of the code of the client and the server. Are you sure that your first form is closing the connection when it''s finished? You also state that you are not sure if the server is staying open or even listening, so how can you expect it to work?


我认为您的服务器代码只能接受一个客户端连接.下一行:
I think your server code is only able to accept a single client connection. The following line:
TcpClient c = l.AcceptTcpClient();


在程序的开头仅被调用一次,因此,在Form1创建连接后,服务器将不再检查该连接.同样,您的while(true)循环似乎没有任何中断的方式,从而阻塞了整个程序.您需要某种方式来表示与Form1的通信结束,然后返回等待客户端连接,或者将您的处理分为多个线程以允许同时进行多个连接.


is only called once, at the beginning of the program, so as soon as Form1 creates a connection the server will not check for any more. Also your while(true) loop does not appear to have any way to break out, thus blocking the whole program. You need some way to signal the end of the communication with Form1 and go back to waiting for a client connection, or separate your processing into threads to allow multiple connections at the same time.


这篇关于请帮助,C#客户端服务器编程中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆