Hey guys, I need a really simple code for chatting across consoles. I have looked up many tutorials on how to do this, yet they all seem to fail.

All I want is a VERY simple chat that sends data from the client to a file, and then receives the text back from the file. However through all the research I have done, the Webrequest POST method is the only method I found that can 'write' to a file on a server. Unfortunately, that doesn't even work!

Code:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestPostExample
    {
        public static void Main()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("http://www.terrariarvb.com/accept.aspx");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
    }
}

This 'should' write, "This is a test that posts this string to a Web server." on the file, right? Well it doesn't. I set the file permissions to 7777, yet I can't get ANYTHING to write to it..it just remains blank.

Any help would be appreciated and I will be sure to show my gratitude to anyone who can help me with this.

Well, what is accept.aspx doing with the posted data? You can't just send data to the URL and expect it do what you want with it.
If you want to write to a file on the server, accept.aspx will need to get the posted data from the request and then save it to the correct location. Have you coded for it to do this?

OK, cool. That example shows how to attach data to a web request and send it to a webpage. But what is the accept.aspx doing with the posted data? As I said before the receiving page needs to do something with the posted data. Have you added any code to your accept.aspx page to take care of saving the data? Where do you specify what file to save to?

for ease and full control over your chat I suggest you develop a protocol for your server/client first.
Avoid using the System.Web members.
Rather use the System.Net.Socket members and more specifically use the Socket class or TcpListener and TcpClient for server and client respectively.
That way if your protocol is good enough you can even avoid developing a client for your chat and rather use the Telnet application to chat and send files over the network.

Check the OSI and the MSDN documentation for that. I'll post an example code in my next post.

NOTE this code might not be perfect as I'm just using a normal text editor not vs

//code example for Server
using System;
using System.Net;
using System.Net.Socket;//check if the namespaces are correct
using System.Threading;//check if the namespaces are correct
using System.Collection.Generic;//check if the namespaces are correct

namespace chat
{
 class server
{

private static TcpLister serverSocket;
private static TcpClient client;
private static List<Socket> clients = new List<Socket>();
const int PORT = 3306;//choose a different port if you have Mysql server
private static bool running=true;
private static Thread serverThread;

static void Main()
{
 serverSocket = new TcpListener(PORT,IpAddress.Any);
 serverSocket.Start();
 
 while(running)
{
//server will block here until a connection has been established and accepted
  client = serverSocket.Accept();//can't remember the exact method name
//from here, we assume a client has connected
clients.Add(client);// add the client to the client's collection
serverThread = new Thread(new ThreadStat(Clienthandler));
serverThread.Start();
//if you are having too much traffic and would love to Sync your clients you can
//try making them run in parallel 


}


}

//use this to handle a newly connected cleint
protected void Clienthandler()
{
//do something with the client connection, after all you have access to the 
//client's collection and the client object

}







}
}

I can write you the whole application but I hope you see a green light.
Happy coding mate

That is pretty cool, however I can't make it work across the web, as the program uses my IP -- and with others it would use theirs. How might I go about setting it up to send and receive data from a mysql server?

But if you could write be the program and explain how to attach it to a mysql server, I would definitely pay you 20$ or so. If you want the payment in advance, send me a PM.

Thanks for helping! =]

Bump. Can someone help me with this? Or point me to a youtube video or something that explains it from step 1.

I really just want my own VERY simple console IRC.

Does no one know how to do this? Or am I just overly dumb and can't see the solution in front of my face?

This article has been dead for over six months. Start a new discussion instead.