?
Solved

C++ Socket Programming

Posted on 2003-03-12
7
Medium Priority
?
216 Views
Last Modified: 2010-04-01
I need some sample codes for C++ to open a Socket to be able to send and receive a String.
0
Comment
Question by:santedesprit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8120032

OpenSocket();

ConnectSocket();

ListenSocket();


0
 
LVL 2

Expert Comment

by:keenez
ID: 8120228
This sounds like homework so we can only point you towards the solution.  The answer from Kdo might be the best you'll get.

Cheers,

Keenez
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:romanm
ID: 8124298
// startup
WORD wVersionRequested;
WSADATA wsaData;
     wVersionRequested = MAKEWORD( 2, 0 );

if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
     // here you do all work

     .....

     WSACleanup( );
}

// creating socket to listen and receive
struct sockaddr_in sin;
memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(1000);
SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
bind(s,(struct sockaddr *)&sin, sizeof(sin));
int listen(s,SOMAXCONN);
SOCKET a;
struct sockaddr_in acAddr = {0};
int len = sizeof(sockaddr_in);
a = accept(a, (struct sockaddr *)&acAddr, &len);
char data[100];
int bytesreceived = recv(a,data,100,0);// 100 is max number of bytes

// creating socket to send
struct sockaddr_in sin;
memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
sin.sin_port = htons(1000);
SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
connect(s, (struct sockaddr *)&sin,sizeof(sin));
char data[] = "hello world.";
send(s, data, strlen(data),0);

// after you finish with the socket remember
closesocket(s);


thats about it,
cheers
-roman
0
 
LVL 4

Expert Comment

by:havman56
ID: 8126087
see my website i have hosted a free socket program having client and server.
it works fine.

http://micro_mangal.tripod.com
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9481885
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ/No Refund Points(Homework)

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9537050
This question is PAQed and no points refunded (of 70)

YensidMod
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question