Socket connections.

Discussion in 'Programming' started by Teh-Uploader, Jul 27, 2007.

Thread Status:
Not open for further replies.
  1. #1
    Trying to build a file resaving program

    Here is my code

    Main.cpp
    #include "wcomm.h"
    
    
    void runclient(char *ip, char *fpath);
    void runserver();
    
    WComm w;
    
    void main(int argc, char *argv[])
    {
    	if(argc==1)runserver();
    	else runclient(argv[1],argv[2]);
    }
    
    
    void runserver()
    {
    	// Start Server Daemon
    	w.startServer(27015);
    	printf("Server Started........\n");
        while (TRUE) {
    		// Wait until a client connects
    		w.waitForClient();
    		printf("Client Connected......\n");
    
    		// Work with client
    		while(TRUE)
    		{
    			char rec[50] = "";
    			w.recvData(rec,32);w.sendData("OK");
    
    			if(strcmp(rec,"FileSend")==0)
    			{
    				char fname[32] ="";
    				w.fileReceive(fname);
    				printf("File Received.........\n");
    			}
    			if(strcmp(rec,"EndConnection")==0)break;
    			printf("Connection Ended......\n");
    		}
    		// Disconnect client
    		w.closeConnection();
    	}
    }
    
    void runclient(char *ip, char *fpath)
    {
    	char rec[32] = "";
    	// Connect To Server
    	w.connectServer(ip,27015);
    	printf("Connected to server...\n");
    	// Sending File
    	w.sendData("FileSend");	w.recvData(rec,32);
    	w.fileSend(fpath);
    	printf("File Sent.............\n");
    	// Send Close Connection Signal
    	w.sendData("EndConnection");w.recvData(rec,32);
    	printf("Connection ended......\n");
    }
    Code (markup):

    form1
    #pragma once
    
    
    namespace Updateit {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary>
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the
    	///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::Button^  button1;
    
    	private: System::Windows::Forms::ProgressBar^  progressBar1;
    	private: System::Windows::Forms::Button^  button2;
    	private: System::Windows::Forms::TextBox^  textBox1;
    
    
    
    	protected: 
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->button1 = (gcnew System::Windows::Forms::Button());
    			this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
    			this->button2 = (gcnew System::Windows::Forms::Button());
    			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
    			this->SuspendLayout();
    			// 
    			// button1
    			// 
    			this->button1->Location = System::Drawing::Point(12, 12);
    			this->button1->Name = L"button1";
    			this->button1->Size = System::Drawing::Size(51, 29);
    			this->button1->TabIndex = 0;
    			this->button1->Text = L"Update";
    			this->button1->UseVisualStyleBackColor = true;
    			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    			// 
    			// progressBar1
    			// 
    			this->progressBar1->Location = System::Drawing::Point(60, 156);
    			this->progressBar1->Name = L"progressBar1";
    			this->progressBar1->Size = System::Drawing::Size(613, 20);
    			this->progressBar1->TabIndex = 2;
    			this->progressBar1->Click += gcnew System::EventHandler(this, &Form1::progressBar1_Click);
    			// 
    			// button2
    			// 
    			this->button2->Location = System::Drawing::Point(69, 12);
    			this->button2->Name = L"button2";
    			this->button2->Size = System::Drawing::Size(51, 29);
    			this->button2->TabIndex = 3;
    			this->button2->Text = L"Cancel";
    			this->button2->UseVisualStyleBackColor = true;
    			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
    			// 
    			// textBox1
    			// 
    			this->textBox1->Location = System::Drawing::Point(134, 108);
    			this->textBox1->Name = L"textBox1";
    			this->textBox1->Size = System::Drawing::Size(479, 20);
    			this->textBox1->TabIndex = 4;
    			this->textBox1->Text = L"Welcome, we will now begin updating your software please wait. Click update to ge" 
    				L"t started.";
    			this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(729, 269);
    			this->Controls->Add(this->textBox1);
    			this->Controls->Add(this->button2);
    			this->Controls->Add(this->progressBar1);
    			this->Controls->Add(this->button1);
    			this->Name = L"Form1";
    			this->Text = L"Shellfish Update";
    			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    			this->ResumeLayout(false);
    			this->PerformLayout();
    
    		}
    #pragma endregion
    	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
    
    
    			 }
    	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    }		
    	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    			 }
    private: System::Void richTextBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
    		 }
    private: System::Void webBrowser1_DocumentCompleted(System::Object^  sender, System::Windows::Forms::WebBrowserDocumentCompletedEventArgs^  e) {
    		 }
    private: System::Void progressBar1_Click(System::Object^  sender, System::EventArgs^  e) {
    		 }
    private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
    		 }//CONNECT TO REMOTE HOST (CLIENT APPLICATION)
    //Include the needed header files.
    //Don't forget to link libws2_32.a to your program as well
    #include <winsock.h>
     #include "main.cpp"
    
    SOCKET s; //Socket handle
    
    //CONNECTTOHOST – Connects to a remote host
    bool ConnectToHost(int PortNo, char* IPAddress)
    {
        //Start up Winsock…
        WSADATA wsadata;
    
        int error = WSAStartup(0x0202, &wsadata);
    
        //Did something happen?
        if (error)
            return false;
    
        //Did we get the right Winsock version?
        If (wssadata.wVersion != 0x0202)
        {
            WSACleanup(); //Clean up Winsock
            return false;
        }
    
        //Fill out the information needed to initialize a socket…
        SOCKADDR_IN target; //Socket address information
    
        target.sin_family = AF_INET; // address family Internet
        target.sin_port = htons (PortNo); //Port to connect on
        target.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
    
        s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
        if (s == INVALID_SOCKET)
        {
            return false; //Couldn't create the socket
        }  
    
        //Try connecting...
    
        if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
        {
            return false; //Couldn't connect
        }
        else
            return true; //Success
    }
    
    //CLOSECONNECTION – shuts down the socket and closes any connection on it
    void CloseConnection ()
    {
        //Close the socket if it exists
        if (s)
            closesocket(s);
    
        WSACleanup(); //Clean up Winsock
    }
    Code (markup):
    winsock

    struct sockaddr_in 
    {
          short      sin_family;
          u_short      sin_port;
          struct      in_addr sin_addr;
          char      sin_zero[8];
    };
     
    int PASCAL connect(SOCKET,const struct sockaddr*,int);
    Code (markup):

    I don't get how to connect that all together + get it connected to my server.
     
    Teh-Uploader, Jul 27, 2007 IP
  2. Teh-Uploader

    Teh-Uploader Peon

    Messages:
    1,118
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can not one persn help me?
     
    Teh-Uploader, Jul 27, 2007 IP
  3. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Looking at it, the top part looks to be a socket listener, listening on a particular port.

    To do that you'd need the code compiled and installed as a service on your server.

    However, the complexity of talking you through the task would be likely too great as your question, without wishing to be rude, implies you don't know too much about the code?

    What exactly do you mean by a "file re-saving" program? What's it meant to do?
     
    ecentricNick, Jul 28, 2007 IP
  4. Teh-Uploader

    Teh-Uploader Peon

    Messages:
    1,118
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Was ment to be a resaving. sorry.

    I don't know much, but I wnat to do it before i go on holiday.

    If someone can help me get it done i'll thank them for life!
     
    Teh-Uploader, Jul 28, 2007 IP
  5. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm still not clear what resaving is?

    So, I can't judge if the code is at all suitable for the task.

    Or, whether there's a far easier way to do this. Because, it's very rare to need to write a socket listener these days. Webservers are so flexible it's not normally required.
     
    ecentricNick, Jul 28, 2007 IP
  6. Teh-Uploader

    Teh-Uploader Peon

    Messages:
    1,118
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well how do I do this?


    becasue this is really anoying me.
     
    Teh-Uploader, Jul 28, 2007 IP
  7. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Please, go back to the start, what is this piece of code meant to do for you? What is the task you are trying to achieve? What is a "resaver"?
     
    ecentricNick, Jul 28, 2007 IP
  8. Teh-Uploader

    Teh-Uploader Peon

    Messages:
    1,118
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    code isn't worth alot? the task is trying to get files sent from the server to the client.
     
    Teh-Uploader, Jul 28, 2007 IP
Thread Status:
Not open for further replies.