In C# making a chat program how would you create an ftp feature -
i have created chat program , created gui ftp.i have coded think work ftp, when run solution keeps giving me different results. is, creates file different memory set each time. here relevant coding, problem appears happen when transfered client:
in server :
//listens new commands coming in client associated client object public void command_listener() { foreach (client c in ui[0].clients) { sw.writeline("2: user: " + username + " has connected..."); sw.flush(); //notifies other client objects inside user interface 1 operational } while (ui[0].busy) { } //waits log not busy task declares task on ui[0].busy = true; ui[0].log_event(username + " has connected..." + datetime.now.tostring()); ui[0].log_event("client username : " + client_info[0]); ui[0].log_event("client computer name : " + client_info[1]); ui[0].log_event("client operating system : " + client_info[2]); ui[0].log_event("client lan ip address : " + client_info[3]); ui[0].log_event("client wan ip address : " + client_info[4]); //sends client info client server's user interface , log ui[0].busy = false; while (true) { try { string command = sr.readline(); //waits new message if (command[0] == '1') update_chat_list(command.replace("1: ", "")); //determines username has changed , needs update list if (command[0] == '2') update_message_board(command.replace("2: ", "")); //determines message board on each clients needs update new command included if (command == "3: typing" && !typing) //determines client's typing { typing = true; update_chat_list(username); } if (command == "3: empty" && typing) //determine client's not typing { typing = false; update_chat_list(username); } if (command[0] == '5') //determines new ftp in progress update_ftp_status(command.replace("5: ", "")); if (command[0] == '6') { //adds incoming kilobyte of file string list kilos.add(command.replace("<heartbeat>", "")); } if (command[0] == '7') { //tells server pieces of file have been recieved kilos.add(command); new thread(send_file).start(); } } catch //catchs listening problems , tells server client must removed , user list update , logs exit { sw.dispose(); sr.dispose(); ui[0].clients.removeat(client_index); ui[0].update_index(); while (ui[0].busy) { } ui[0].busy = true; ui[0].log_event(username + " has disconnected..."); ui[0].busy = false; foreach (client c in ui[0].clients) { c.sw.writeline("2: " + username + " has disconnected..."); c.sw.flush(); } update_chat_list(""); return; } } } public void send_file() { string last = ""; foreach (string kilo in kilos) { ftp_connection[0].sw.writeline(kilo); ftp_connection[0].sw.flush(); //sends part of file , closing compile command client } ftping = false; int length = last.length; ftp_connection[0].ftping = false; //changes ftp status off } public void update_ftp_status(string user) { string filename = ""; bool building = false; foreach (char c in user) { if (building) filename = filename + c; else if (c == ':') building = true; } //retrieves filename user_to_send_to = user.replace(":" + filename, ""); //replaces filename out of message user send foreach (client user in ui[0].clients) { if (user.username == user_to_send_to) { user.sw.writeline("5: " + username); user.sw.flush(); user.sw.writeline(filename); user.sw.flush(); ftp_connection.add(user); //notifies client recieving file setups direct access client ftp going to. } } }
in client :
//listens new commands given server void command_listener() { connected = true; while (true) { try { string comunicae = sr.readline(); //recieves message if (comunicae[0] == '1') user_list_update(comunicae.replace("1: ", "")); //determines user list has updated if (comunicae[0] == '2') new_message(comunicae.replace("2: ", "")); //determines messages board has updated if (comunicae[0] == '4') new_server_command(comunicae.replace("4: ", "")); if (comunicae[0] == '5') //notifies user theres new ftp incoming { ftping = true; string filenam = sr.readline(); file_name = filenam; ftp_sender = comunicae.replace("5: ", ""); recipients_message = comunicae.replace("5: ", "") + " sending " + filenam + " file via ftp...\r\nyou recieve file in " + settings[0].driver + "program data\\"; } if (comunicae[0] == '6') { int length = comunicae.length; lengths.add(length); string kilo = comunicae.replace("6: ", ""); kilos.add(kilo); //gathers incoming file parts } if (comunicae[0] == '7') { new thread(compile_file).start(); //pieces file } } catch (exception e) { //kills client if theres reading error process.getcurrentprocess().kill(); return; } } } void compile_file() { int length = kilos[kilos.count - 1].length; list<byte> file = new list<byte>(); foreach (string kilo in kilos) //seperates out each byte in each file part adds byte list { string byt = ""; int helper = 0; foreach (char c in kilo) { if (helper == 3) { string temp = byt; file.add(convert.tobyte(temp)); byt = ""; helper = 0; } if (c != '|') { byt = byt + c; } helper++; } } byte[] final = new byte[file.count - 1]; (int = 0; != file.count - 1; i++) //converts byte list array { final[i] = file[i]; } file.writeallbytes(settings[0].driver + "program data\\" + file_name, final); //writes file ftping = false; } private void send_click(object sender, eventargs e) { if (controler[0].ftping) //detects transfers in progress { messagebox.show("you have file transfer in progress..."); return; } if (ftping_users.contains(user_to_send_to.text)) //detects if user try send has file inbound { messagebox.show(user_to_send_to.text + " has inbound or outbound file in progress..."); return; } controler[0].update_ftp_status(user_to_send_to.text, selected_file); //aprises server of new ftp in progress byte[] file = file.readallbytes(current_directory.text.replace(fil, "")); //reads file thread t = new thread(() => get_kilos(file)); t.start(); //starts segmenting file bytes , converting them strings transfer this.hide(); } public void get_kilos(byte[] file) { list<string> kilos = new list<string>(); string kilobyte = ""; foreach (byte b in file) { string byt = (convert.toint32(b)).tostring(); if (byt.length == 1) byt = "||" + byt; if (byt.length == 2) byt = "|" + byt; kilobyte = kilobyte + byt; //makes bytes string length standard 3 characters if (kilobyte.length == 3072) { string temp = "6: " + kilobyte; kilos.add(temp); kilobyte = ""; //adds part list of file parts } } kilos.add("6: " + kilobyte); foreach (string kilo in kilos) { int length = kilo.length; controler[0].sw.writeline(kilo); controler[0].sw.flush(); //sends file parts } controler[0].sw.writeline("7: complete..."); controler[0].sw.flush(); //lets server know transfer complete controler[0].ftping = false; }
i have created chat program , created gui ftp.i have coded think work ftp, when run solution keeps giving me different results. is, creates file different memory set each time. here relevant coding, problem appears happen when transfered client: in server : in client : | |||||||||||||||||
|