Computing.Net > Forums > Programming > c++ source

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

c++ source

Reply to Message Icon

Name: Ne0
Date: January 28, 2004 at 06:38:20 Pacific
OS: win: XP, LH, 2K, ME
CPU/Ram: 2300/512
Comment:

#include <iostream.h>
#include <map>
#include <sys\socket.h>


int main()
{
int sockfd;
struct sockaddr_in myaddr;

myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(3490);
myaddr.sin_addr.s_addr=INADDR_ANY;
memset(&(myaddr.sin_zero),'\0',8);

bind(sockfd,(struct sockaddr *)&myaddr,sizeof(struct sockaddr));
listen(sockfd,20);


int sin_size = sizeof(struct sockaddr_in);
int new_fd = accept(sockfd,(struct sockaddr *)&clientaddr,&sin_size);

char *msg = message;
cin >> message;
int len=strlen(msg);
send(new_fd,msg,len,0);

char buffer[250];
int gone = recv(new_fd, buffer, sizeof(buffer), 0);

return 0;
}


can u see what i'm doing the wrong way?
this is NOT my code.. i just want to test it!(then modify)



Sponsored Link
Ads by Google

Response Number 1
Name: Sord
Date: January 28, 2004 at 13:53:16 Pacific
Reply:

What errors are you getting or what runtime problems?


0

Response Number 2
Name: Ne0
Date: January 28, 2004 at 15:04:03 Pacific
Reply:

--------------------Configuration: Socket - Win32 Debug--------------------
Compiling...
Socket.cpp
D:\Win_sock_chat_server\Socket.cpp(21) : error C2065: 'clientaddr' : undeclared identifier
D:\Win_sock_chat_server\Socket.cpp(23) : error C2065: 'message' : undeclared identifier
D:\Win_sock_chat_server\Socket.cpp(23) : error C2440: 'initializing' : cannot convert from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

Socket.obj - 3 error(s), 0 warning(s)


i can fix that by putting all the int and chars up in the right direction but it has a linking prblem too...

i'll try to get the other error tomoroow..

hope u can help me a little


0

Response Number 3
Name: Ne0
Date: January 29, 2004 at 02:27:05 Pacific
Reply:

--------------------Configuration: Socket - Win32 Debug--------------------
Compiling...
Socket.cpp
C:\cpp\Socket_Win\Socket.cpp(14) : warning C4700: local variable 'sockfd' used without having been initialized
Linking...
Socket.obj : error LNK2001: unresolved external symbol __imp__recv@16
Socket.obj : error LNK2001: unresolved external symbol __imp__send@16
Socket.obj : error LNK2001: unresolved external symbol __imp__accept@12
Socket.obj : error LNK2001: unresolved external symbol __imp__listen@8
Socket.obj : error LNK2001: unresolved external symbol __imp__bind@12
Socket.obj : error LNK2001: unresolved external symbol __imp__htons@4
Debug/Socket.exe : fatal error LNK1120: 6 unresolved externals
Error executing link.exe.

Socket.exe - 7 error(s), 1 warning(s)


thats what i dont understand...

hope u can help me with this cos it's a school project and i want to learn this a bit faster.


0

Response Number 4
Name: Ne0
Date: January 29, 2004 at 02:48:31 Pacific
Reply:

--------------------Configuration: Socket - Win32 Debug--------------------
Compiling...
Socket.cpp
C:\cpp\Socket_Win\Socket.cpp(14) : warning C4700: local variable 'sockfd' used without having been initialized
C:\cpp\Socket_Win\Socket.cpp(21) : warning C4700: local variable 'new_fd' used without having been initialized
C:\cpp\Socket_Win\Socket.cpp(25) : warning C4700: local variable 'msg' used without having been initialized
Linking...
Socket.obj : error LNK2001: unresolved external symbol __imp__send@16
Socket.obj : error LNK2001: unresolved external symbol __imp__accept@12
Socket.obj : error LNK2001: unresolved external symbol __imp__recv@16
Socket.obj : error LNK2001: unresolved external symbol __imp__listen@8
Socket.obj : error LNK2001: unresolved external symbol __imp__bind@12
Socket.obj : error LNK2001: unresolved external symbol __imp__htons@4
Debug/Socket.exe : fatal error LNK1120: 6 unresolved externals
Error executing link.exe.

Socket.exe - 7 error(s), 3 warning(s)

i fixed some more and noe i get this after compiling this:

#include <map>
#include <iostream.h>
#include <sys\socket.h>
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in myaddr;

myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(3490);
myaddr.sin_addr.s_addr=INADDR_ANY;
memset(&(myaddr.sin_zero),'\0',8);

bind(sockfd,(struct sockaddr *)&myaddr,sizeof(struct sockaddr));

listen(sockfd,20);

int sin_size = sizeof(struct sockaddr_in);
int new_fd;
char buffer[250];
int clientaddr = recv(new_fd, buffer, sizeof(buffer), 0);
new_fd = accept(sockfd,(struct sockaddr *)&clientaddr,&sin_size);

char *msg;
cin >> *msg;
int len = strlen(msg);
send(new_fd,msg,len,0);

return 0;
}

tnx for any answers


0

Response Number 5
Name: Ne0
Date: January 29, 2004 at 10:12:43 Pacific
Reply:

i think i know what the fault is... the place where i downloaded the socket.h file
said:

When you compile this files, make sure you link them against Ws2_32.lib.

how can i link them to that?
cos it is a linking error..

i'm using visual c++ 6


0

Related Posts

See More



Response Number 6
Name: Cyber Bandit
Date: January 29, 2004 at 19:43:16 Pacific
Reply:

For this u have to go to menu Project->Settings->Link->Object/Library modules->(Here add Ws2_32.lib)
I hope this will solve ur linker problem


0

Response Number 7
Name: Ne0
Date: January 30, 2004 at 06:57:52 Pacific
Reply:

thanks. it got rid of the linking errors..

i have dont some new things to it now.

this is how it looks like:


#include "stdafx.h"
#include <socket.h>
#include <winsock2.h>

int main(int argc, char* argv[])
{

int sockfd;
struct sockaddr_in myaddr;

myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(90);
myaddr.sin_addr.s_addr=INADDR_ANY;
memset(&(myaddr.sin_zero),'\0',8);

bind(sockfd,(struct sockaddr *)&myaddr,sizeof(struct sockaddr));
listen(sockfd,20);

int clientaddr;
int sin_size = sizeof(struct sockaddr_in);
int new_fd = accept(sockfd,(struct sockaddr *)&clientaddr,&sin_size);

char *msg = "Hey nut crack!";
int len=strlen(msg);
send(new_fd,msg,len,0);


return 0;
}

that is about exactly how the tutorial said i shuold have done it.

but when i run it it doesent happen anything...

i get a warning.. this is it:

--------------------Configuration: chat - Win32 Debug--------------------
Compiling...
chat.cpp
D:\chat\chat.cpp(19) : warning C4700: local variable 'sockfd' used without having been initialized
Linking...

chat.exe - 0 error(s), 1 warning(s)


hope u can help more with this...


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: c++ source

How can I convert C++ source code into J www.computing.net/answers/programming/how-can-i-convert-c-source-code-into-j/1001.html

C++ Source Code for MP3 Player www.computing.net/answers/programming/c-source-code-for-mp3-player/8452.html

C++ source code www.computing.net/answers/programming/c-source-code/8029.html