Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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...
Posted
Comments
F-ES Sitecore 24-Jul-15 7:57am    
If you can't connect then it is a network issue rather than a code issue. Check for firewalls blocking non-standard ports etc.
Richard MacCutchan 24-Jul-15 8:31am    
Google will find you many samples of socket code that you can modify to your own requirements.

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!
 
Share this answer
 
Comments
Umut Comlekcioglu 24-Jul-15 11:38am    
No, no... I don't want to send all project to me. Just I want to What I need to do?

For example, in server side I write;

IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[ipHostInfo.AddressList.Length - 1];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11001);

listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

listener.Bind(localEndPoint);
listener.Listen(100);

listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);









And Client Side;
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("My Computer IP"), 11001);

Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);



But I'm still cannot connect to server.. What's my wrong?
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.

C#
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;
     }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900