473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Errors in socket program

4
Hi I'm new to C++ and trying to create a simple socket server but I keep getting 3 errors which I don't understand. Below is the code, please help:
Expand|Select|Wrap|Line Numbers
  1. #include "ServerSocket.h"
  2. #include "SocketException.h"
  3. #include <iostream.h>
  4. #include <string>
  5.  
  6. int main ( int argc, int argv[] )
  7. {
  8.  
  9.   try
  10.     {
  11.       // Create the socket
  12.       ServerSocket server ( 30000 );
  13.  
  14.       while ( true )
  15.     {
  16.  
  17.       ServerSocket new_sock;
  18.       server.accept ( new_sock );
  19.  
  20.       try
  21.         {
  22.           while ( true )
  23.         {
  24.           std::string data;
  25.           new_sock >> data;
  26.           new_sock << data;
  27.         }
  28.         }
  29.       catch ( SocketException& ) {}
  30.  
  31.     }
  32.     }
  33.   catch ( SocketException& e )
  34.     {
  35.       std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
  36.     }
  37.  
  38.   return 0;
  39. }
  40.  
Here's the error they keep showing:

c:\documents and settings\studentivtl\my documents\cpp1.cpp(8) : error C2039: 'cout' : is not a member of 'std'
c:\documents and settings\studentivtl\my documents\cpp1.cpp(36) : error C2039: 'cout' : is not a member of 'std'
c:\documents and settings\studentivtl\my documents\cpp1.cpp(36) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or t
here is no acceptable conversion)
Error executing cl.exe.
Sep 19 '07 #1
8 2456
gpraghuram
1,275 Expert 1GB
Hi I'm new to C++ and trying to create a simple socket server but I keep getting 3 errors which I don't understand. Below is the code, please help:

#include "ServerSocket.h"
#include "SocketException.h"
#include <iostream.h>
#include <string>

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

try
{
// Create the socket
ServerSocket server ( 30000 );

while ( true )
{

ServerSocket new_sock;
server.accept ( new_sock );

try
{
while ( true )
{
std::string data;
new_sock >> data;
new_sock << data;
}
}
catch ( SocketException& ) {}

}
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}

return 0;
}

Here's the error they keep showing:

c:\documents and settings\studentivtl\my documents\cpp1.cpp(8) : error C2039: 'cout' : is not a member of 'std'
c:\documents and settings\studentivtl\my documents\cpp1.cpp(36) : error C2039: 'cout' : is not a member of 'std'
c:\documents and settings\studentivtl\my documents\cpp1.cpp(36) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or t
here is no acceptable conversion)
Error executing cl.exe.

Change the #include<iostream.h> to
#include<iostream>.
I think that will solve the error.

Raghuram
Sep 19 '07 #2
zorde
4
Change the #include<iostream.h> to
#include<iostream>.
I think that will solve the error.

Raghuram
I have tried to change it to #include <iostream> and this is the error they come up with

Cpp1.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall ServerSocket::~ServerSocket(void)" (??1ServerSocket@@UAE@XZ)
Cpp1.obj : error LNK2001: unresolved external symbol "public: class ServerSocket const & __thiscall ServerSocket::operator<<(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)const " (??6ServerSocket@@QB
EABV0@ABV?$basic_string@DU?$char_traits@D@std@@V?$ allocator@D@2@@std@@@Z)
Cpp1.obj : error LNK2001: unresolved external symbol "public: class ServerSocket const & __thiscall ServerSocket::operator>>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)const " (??5ServerSocket@@QBEABV0@
AAV?$basic_string@DU?$char_traits@D@std@@V?$alloca tor@D@2@@std@@@Z)
Cpp1.obj : error LNK2001: unresolved external symbol "public: void __thiscall ServerSocket::accept(class ServerSocket &)" (?accept@ServerSocket@@QAEXAAV1@@Z)
Cpp1.obj : error LNK2001: unresolved external symbol "public: __thiscall ServerSocket::ServerSocket(int)" (??0ServerSocket@@QAE@H@Z)
Cpp1.obj : error LNK2001: unresolved external symbol "protected: __thiscall Socket::Socket(void)" (??0Socket@@IAE@XZ)
Debug/Cpp1.exe : fatal error LNK1120: 6 unresolved externals
Sep 19 '07 #3
sicarie
4,677 Expert Mod 4TB
It's also considered good practice to add the namespace line afterwards:
Expand|Select|Wrap|Line Numbers
  1. #include "ServerSocket.h"
  2. #include "SocketException.h"
  3. #include <iostream.h>
  4. #include <string>
  5. using namespace std;
  6.  
Sep 19 '07 #4
zorde
4
It's also considered good practice to add the namespace line afterwards:
Expand|Select|Wrap|Line Numbers
  1. #include "ServerSocket.h"
  2. #include "SocketException.h"
  3. #include <iostream.h>
  4. #include <string>
  5. using namespace std;
  6.  
Hi I've tried to add the "using namespace std;" command too but still having the same errors. Am I missing some files?? Because I'm currently using Visual C++ 6.0 to run it.
Sep 20 '07 #5
sicarie
4,677 Expert Mod 4TB
Hi I've tried to add the "using namespace std;" command too but still having the same errors. Am I missing some files?? Because I'm currently using Visual C++ 6.0 to run it.
My apologies, I did not mean to imply that it was the cause of your errors, it is just good practice. I believe there are other errors in your code, but I don't have my compiler available, so I will not be able to llok at it right now.
Sep 20 '07 #6
RRick
463 Expert 256MB
Changing iostream.h to iostream fixed you cout problem and your code compiled. Now the compiler is trying to link all the pieces together to make a program, and it can't find the compiled code for ServerSocket.

You need to include the library that contains these modules or if its your own code, you need to add ServerSocket.o for the linking.
Sep 20 '07 #7
zorde
4
Changing iostream.h to iostream fixed you cout problem and your code compiled. Now the compiler is trying to link all the pieces together to make a program, and it can't find the compiled code for ServerSocket.

You need to include the library that contains these modules or if its your own code, you need to add ServerSocket.o for the linking.
Thanks, I've included the necessary library for the program. But another problem come out, the compiler can't find #include <sys/socket.h>. Do you know where I can find and download this??
Sep 20 '07 #8
RRick
463 Expert 256MB
sys/socket.h is pretty basic and part of most installations. On linux boxes, the sys directory is located under /usr. I would take a look there before loading socket code. You can also use the find command to see if socket.h is located somewhere else.

If your system is really missing the socket code, go to the manufacturer's web site and see if you can get something already compiled for your system. For linux, there are installation programs (rpm, yum) that will take most of the drudgery out of this task.
Sep 20 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: ReaprZero | last post by:
Hi, I'm using Cygwin and ActiveState perl to try to compile a sample application using SWIG. I'm using the short tutorial from http://www.swig.org/tutorial.html (the perl part of it), but with a...
0
by: Dennis Francis B. Tutanes | last post by:
$B$*@$OC$K$J$C$F$*$j$^$9!#(B $B%D%?%M%9(B@TSTI$B$G$9!#(B SEND-PR: -*- send-pr -*- SEND-PR: Lines starting with `SEND-PR' will be removed automatically, as SEND-PR: will all comments (text...
0
by: Krishnan | last post by:
Hi I am trying to build a small client-server program using OpenSSL functions. I get errors on trying to build the program using Visual Studio .Net (Visual C++ 7.0). I am new to Visual Studio...
2
by: errmaker | last post by:
hi i have msdn tcp/ip sample : #include "stdafx.h" //int _tmain(int argc, _TCHAR* argv) //{ // return 0;
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.