如何制作像聊天程序这样的程序? [英] How can I make a program like Chat program?

查看:73
本文介绍了如何制作像聊天程序这样的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想做一个像聊天程序一样的程序。我需要创建一个服务器,所有其他客户端都依赖于服务器。

例如; Client1想发送消息Client2。首先,消息将发送到服务器和服务器发送消息给Client2。



我有一台3台电脑。我将一台计算机IP地址更改为服务器计算机的静态IP。但是我无法发送消息或者我无法通过客户端连接到服务器。



我想使用套接字。







我在等待帮助。祝你有美好的一天。谢谢你回答......

Hi everyone,

I wanna make a program like a chat program. I need to be create a Server and all other Clients depend on server.
For example; Client1 wanna send message Client2. First the message will send to Server and Server send message to Client2.

I have an 3 computer. I change one computer IP address to Static IP for Server Computer. But I can't send message or I can't connect to Server with client.

I want to use Sockets for this.



I'm waiting for helps. Have a good day. Thanks for answer...

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


Alrigh,我会帮你指明方向。剩下的就是让你找到答案。



为了使沟通成为可能,我们必须设置一个服务器。这将接收来自每个客户端的连接。它是通过倾听来实现的。

由于听取需要经常注意,它会冻结程序,除非你把它放在另一个线程上。



这是几年前我写的聊天程序中的'Connect'类。



Alrigh, I'll just help you into a direction. The rest is for you to find out.

To make communication possible, we have to setup a server. This receives the connections from every client. It does that by listening.
Since listening requires constant attention, it will freeze up the program, unless you place it on another thread.

This is the 'Connect' class from a chat-program I've written some years back.

private string host = "127.0.0.1"; // This is the ip of the server (currently localhost for testing)
private int port = 10445; // portnumber
private TcpClient client;
private NetworkStream stream;
private Byte[] data;

public int Port // So you can change the portnumber from outside the class
{
    get
    {
        return port;
    }
    set
    {
        port = value;
    }
}

public Connect() // Constructor
{
    client = new TcpClient(host, port);
    stream = client.GetStream();
}

public void Send(string msg) // Sends a message
{
    data = System.Text.Encoding.ASCII.GetBytes(msg); 
    stream.Write(data, 0, data.Length);
}

public string Listen() // Listens for messages
{
    Byte[] data2 = new Byte[256];
    String responseData = String.Empty;
    Int32 bytes = 0;

    try
    {
        bytes = stream.Read(data2, 0, data2.Length);
        responseData = System.Text.Encoding.ASCII.GetString(data2, 0, bytes);
        return responseData;
    }
    catch (IOException)
    {
        MessageBox.Show("Lost connection to the sever...\n\nClick ok to exit");
        Application.Exit();
        return null;
     }
}


这篇关于如何制作像聊天程序这样的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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