Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
help me to fix my coding, my output chat too server can't be show, and and vice versa with the client also can not be displayed
this is the client code
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDP_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            int recv2;
            byte[] data = new byte[30];
            byte[] data2 = new byte[1024];
            string input, stringData;
           
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
            Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
           
            string helo = "Connected";
            //newsock.Bind(ipep);

            data = Encoding.ASCII.GetBytes(helo);
            server.SendTo(data, data.Length, SocketFlags.None, ipep);
            
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint tmpRemote = (EndPoint)sender;
            

            data = new byte[30];
            int recv = server.ReceiveFrom(data, ref tmpRemote);
            //recv2 = newsock.ReceiveFrom(data, ref tmpRemote);
            Console.Write("Pesan diterima dari IPAddress {0}:",tmpRemote.ToString());
            Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));


            int i = 30;
            while (true)
            {
                Console.Write("Pesan Saya : ");
                input = Console.ReadLine();
                if (input == "exit")
                break;
                server.SendTo(Encoding.ASCII.GetBytes(input), tmpRemote); //masuk keserver
                data2 = new byte[i];
                try
                {

                    string pesanDariServer = Encoding.ASCII.GetString(data, 0, recv);
                    data2 = Encoding.ASCII.GetBytes(pesanDariServer);
                    newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);

                    recv2 = server.ReceiveFrom(data, ref tmpRemote);
                    stringData = Encoding.ASCII.GetString(data2, 0, recv2);
                    Console.WriteLine("Terkirim ==>> "+stringData);
                }
                catch (SocketException)
                {
                    Console.WriteLine("Perhatian: terjadi kehilangan data");
                    i = i+20;
                }
                
            }
            Console.ReadLine();
            Console.WriteLine("STOP...");
            server.Close();
        }
    }
}


and then this is the server
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDP_Server
{
    class Program
    {
        static void Main(string[] args)
        {
            int recv, recv2;

            byte[] data = new byte[1024];
            byte[] data2 = new byte[1024];
            string input2, stringData2;

            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            newsock.Bind(ipep);
            Console.WriteLine("Menunggu Koneksi Client...");
                        
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint tmpRemote = (EndPoint)(sender);
            recv = newsock.ReceiveFrom(data, ref tmpRemote);

            Console.Write("Pesan diterima dari : {0}:", tmpRemote.ToString());
            Console.WriteLine("===>>" + Encoding.ASCII.GetString(data, 0, recv));

            string welcome = "Anda Tersambung dengan Server";
            data2 = Encoding.ASCII.GetBytes(welcome);
            server.SendTo(data2, data2.Length, SocketFlags.None, tmpRemote);

            

            int i2 = 30;
            while (true)
            {
                //data2 = new byte[1024];
                //recv2 = newsock.ReceiveFrom(data, ref tmpRemote);
                //Console.WriteLine("==>> " + Encoding.ASCII.GetString(data, 0, recv2));
                                
                Console.Write("Pesan Server : ");
                input2 = Console.ReadLine();
                if (input2 == "exit")
                    break;
                newsock.SendTo(Encoding.ASCII.GetBytes(input2), tmpRemote); //masuk keserver
                data = new byte[i2];
                try
                {
                    string pesanDariClient = Encoding.ASCII.GetString(data, 0, recv);
                    data = Encoding.ASCII.GetBytes(pesanDariClient);
                    newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);

                    recv2 = newsock.ReceiveFrom(data, ref tmpRemote);
                    stringData2 = Encoding.ASCII.GetString(data, 0, recv2);
                    Console.WriteLine("Terkirim >> " + stringData2);
                    
                }
                catch (SocketException)
                {
                    Console.WriteLine("Perhatian: terjadi kehilangan data");
                    i2 = i2 + 20;
                }
                
            }
            Console.ReadLine();
            newsock.Close();
            Console.ReadLine();
        }
    }
}
Posted
Comments
Richard MacCutchan 23-Nov-11 16:45pm    
What exactly is the error or errors that you are seeing? It's very difficult to guess what your problem may be.
aikz666dcc 23-Nov-11 20:58pm    
that the output can not display the results of the chat (sorry my english bad)
aikz666dcc 23-Nov-11 21:28pm    
please i need help :(
Addy Tas 24-Nov-11 14:27pm    
Hi, looking at the code i was a bit suprised to see port 0 in the IPEndPoint as this means any available port. Make sure the data send from the client is send to a port on which the server is listening and visa versa.

Hope this helps, if not i'll take a closer look later.

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