Instant messaging server

xiaoxiao2021-03-06  38

This is a simple instant messaging server, the key place I have labeled, I hope to do instant messaging and friends who will be used as a learning material! good luck! Using system; using system.net; using system.text; using system.net.sockets; using system.threading; namespace yjyserver {////

/// timely communication server. /// Class Server {public static socket socket; public static string Str; PUBLIC THREAD THREAD; /// /// Application main inner point.

/// [stathread] static void main (String [] args) {ipaddress serverip = dns.getHostByName ()). addresslist [0]; // Get the native IP address TCPListener Listener = New TCPListener (ServerIP, 8006); // listens 8006 port listener.start (); console.writeline ("server has started, waited for connection .....)"); while (true) // for each new customer Open a service thread {socket = listener.acceptsocket (); // accept the request thread = new thread (New ThreadStart (START)); // Create a customer thread thread.Start (); // Turn on thread // Prepare Next connection }} Public static void start () {INT port = socket.remoteEndPoint.toString (). Indexof (":"); // Get port numbers in the client handle String Remoteip = Socket.RemoteEndPoint.toString (). Substring 0, port); // Get the IP NetworkStream NS = New NetworkStream (Socket) in the customer handle; // Create a network access to the base data stream object StreamReader Reader = New StreamReader (// Creating data streaming object streamwriter Writer = New StreamWriter (NS); // Create a data flow object String Receiv e = reader.readline (); str = receive.substring (0,5); switch (str) {case "hello": Writer.writeline ("ok"); writer.flush (); break; case "bybye" : String name = receive.substring (5, receive.length-5); file.delete ("c: /" name "ip.txt"); goto out; default: goto out;}

IF (reader.readLine () == "Hello") {Writer.writeline ("OK"); Writer.writeLine (Remoteip); Writer.Flush ();} else goto out; // Test connection; str =reader. Readline (); * / str = reader.readLine (); if (File.exists ("C: /" Str ". TXT")) {Writer.writeLine ("exists"); Writer.Flush ();} Else {Writer.writeline ("noexists"); Writer.Flush (); goto out;} // detect username; string passwd = reader.readline (); streamreader file = file.opentext ("c: /" STR ".TXT");

IF (file.readline () == passwd) {Writer.writeline ("Connected"); Writer.Flush (); filestream f = file.openwrite ("c: /" STR "IP.txt"); streamwriter Write = New streamwriter (f); write.close (); f.close ();} else {Writer.writeline ("error"); Writer.Flush (); file.close (); Goto out;} // detection password; string [] logged = new string [10]; int x; for (x = 0; x ) {logged [x] = file.readline (); if (file.peek )! = - 1) Continue; Else Break;} file.close (); int con = 0; BOOL EXIT = FALSE; while (Reader.Readline () == "get") {for (int y = con; <= x; y ) {IF (file.exists ("c: /" logged [y] "ip.txt")) {Writer.WriteLine (Logged [Y]); Writer.Flush (); Reader. Readline (); StreamReader r = file.opentext ("c: /" logged [y] "ip.txt"); Writer.writeLine (R.Readline ()); Writer.Flush (); r.close ); // Close the customer file; * / con = y 1; Break;} else {if (y == x) {EXIT = true; Break; Else {Con = Y 1; Continue;}}} f (exit == True) Break;} Writer.writeLine ("over"); Writer.Flush (); out: console.writeLine ("Exit ..." }}

}

转载请注明原文地址:https://www.9cbs.com/read-71458.html

New Post(0)