Thread: problem in connect code

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    problem in connect code

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <winsock.h>
    #include <stdlib.h>//for NULL coz its macro is declared in it
    #pragma comment(lib,"wsock32.lib")
    
    void connect_ftp()
    {
    	char ftpname[]="74.50.26.15";//just test addr
    	WORD sockVersion;
    	WSADATA wsaData;
    	SOCKET badanie;//intilizing our we gonna use in connecting to ftp
    	//USE SOCKADDR_IN struct to fill in address information
    	SOCKADDR_IN sockinfo;
    	int errorwhy;
    	sockVersion=MAKEWORD(2,2);
    	/*NEXT WE BEGING INITLIZING WINSOCK*/
    	WSAStartup(sockVersion,&wsaData);
    	badanie=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(badanie!=0)
    	{
    		errorwhy=WSAGetLastError();	
    		exit(0);
    	}
    	sockinfo.sin_family=AF_INET;//says we wanna use TCP/IP
    	sockinfo.sin_addr=inet_addr(ftpname);
    	sockinfo.sin_port=htons(21);
    	connect(badanie,(struct SOCKADDR_IN *)&sockinfo,sizeof sockinfo);
    	if(connect==0)  puts("Conncted");
    	puts("PW wrong");
    
    }
    int main(void)
    {
    	connect_ftp();
    	return 0;
    }
    it gives me those errors

    D:\all PRO program you did\projects\connect to ftp\main.c(26) : error C2115: '=' : incompatible types
    D:\all PRO program you did\projects\connect to ftp\main.c(28) : warning C4133: 'function' : incompatible types - from 'struct SOCKADDR_IN *' to 'const struct sockaddr *'
    Error executing cl.exe.

    isnt those suppose = for like the ip it will connect 2 ?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A sockaddr and a sockaddr_in are not the same thing, which I think you know. But they almost are. Your problem is probably this line (b/t/w, I cannot imagine a compiler with more useless output than this one).
    Code:
    connect(badanie,(struct SOCKADDR_IN *)&sockinfo,sizeof sockinfo);
    You probably want to cast as a (struct sockaddr*) instead. Also (I don't know if this matters, but...) I would have used inet_aton here:
    Code:
    sockinfo.sin_addr=inet_addr(ftpname);
    But the windows API is slightly different for sockets, methinks. Someone else may confirm or deny.
    Last edited by MK27; 02-23-2009 at 08:59 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh yah and also i made mistake with connect
    Code:
    if (connect(badanie,(SOCKADDR_IN*)&sockinfo,sizeof sockinfo) == 0) { puts("Connected"); } else puts("........ YOU !!!");
    coz it doesnt return a value

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i still get the error now
    D:\all PRO program you did\projects\connect to ftp\main.c(21) : error C2115: '=' : incompatible types

    i dunno why

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lolguy View Post
    i still get the error now
    D:\all PRO program you did\projects\connect to ftp\main.c(21) : error C2115: '=' : incompatible types

    i dunno why
    Presumably you have changed the code a bit, since it's now on a line 5 lines away from your original post...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i think incompatible types coz i made it an array in the function says its a const char ptr i made it this const char *ftpname="74.50.26.15";//just test addr
    and i still get error

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    connect() returns 0 except on error.

    My only guess about the "incompatible types" (which I found the line numbers in your compiler output now, so I am probably right) was the edit in my last post which you may have missed, regarding the use of inet_aton
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh yah it works now i changed this
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <winsock.h>
    #include <stdlib.h>//for NULL coz its macro is declared in it
    #pragma comment(lib,"wsock32.lib")
    
    void connect_ftp()
    {
    	const char *ftpname="74.50.26.15";//just test addr
    	WORD sockVersion;
    	WSADATA wsaData;
    	SOCKET badanie;//intilizing our we gonna use in connecting to ftp
    	//USE SOCKADDR_IN struct to fill in address information
    	SOCKADDR_IN sockinfo;
    	sockVersion=MAKEWORD(2,2);
    	/*NEXT WE BEGING INITLIZING WINSOCK*/
    	WSAStartup(sockVersion,&wsaData);
    	badanie=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	sockinfo.sin_family=AF_INET;//says we wanna use TCP/IP
    	sockinfo.sin_addr.s_addr=inet_addr(ftpname);
    	sockinfo.sin_port=htons(21);
    	if (connect(badanie,(struct sockaddr*)&sockinfo,sizeof sockinfo) == 0) { puts("Connected"); } 
    		else 
    			puts("............ YOU !!!");
    }
    int main(void)
    {
    	connect_ftp();
    	return 0;
    }
    but it says alawys connected and i dont axx to it since its just a test addr did i ........ up somewhere in the code?

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i wanted to ask sockinfo.sin_addr.s_addr why is it 2 structure i fixed coz i saw in some code he using two . is it double structure or what ? can u do double structure ?

  10. #10
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    If it returns 0, you got connected, which from what I can decipher from that previous post is probably what is happening.

    Also I tested it myself, and it works. If your worried about your program ending, you need to call a blocking function so that it won't. (recv, select, poll, etc)

    What exactly are you trying to say in your last post?

    Edit: Was typing during your post.

    A C struct can actually contain structs inside of itself. It just so happens that the struct you pass to connect has some structs inside of it that you need to manipulate. If you want a very basic example that uses this, look at a singly linked list.
    Last edited by carrotcake1029; 02-23-2009 at 09:19 AM.

  11. #11
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    lolguy, maybe you want to have a look at Beej's Guide to Network Programming, which I consider to be the best introduction to the topic available so far. It's quite UNIX-specific, but applying the advices from chapter 1.5, you should be able to run most of the examples. Most programmers consider network programming in general to be a highly advanced topic, so don't expect too much from this board until you're familiar with the fundamental principles, data structures and so on.

    (b/t/w, I cannot imagine a compiler with more useless output than this one)
    See here: http://www.ralentz.com/old/mac/humor/mpw-c-errors.html

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You probably won't know until you try it for real. Networking code is tricky. Yes there are two structures inside each other -- they are defined in one of those includes, if you want to look.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh oki thanks guys there also http://johnnie.jerrata.com/winsocktutorial/ i ill start with it then i ill go to beej
    most books i saw are unix based

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    On windows you cannot use local sockets, which is a shame because they are slightly simpler and easier to experiment with. But (as you may know already) if you have two computers on a router at home you can use their LAN addresses (192.168...) and do complete TCP/IP networking using little clients and servers. That means
    1) If anyone else lives there and uses them, you can do some almost frightening things.
    2) You can be sure everything works before you unleash your program upon the world.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i also figured out why it alawys says connected coz it connected not loged in

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with network code
    By cornholio in forum Linux Programming
    Replies: 1
    Last Post: 12-20-2005, 01:21 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Replies: 5
    Last Post: 09-28-2004, 12:38 PM
  4. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM