Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
#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)

--------------------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

--------------------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.

--------------------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

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

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

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...

![]() |
![]() |
![]() |

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