[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

C++ Socket Programming

I need some sample codes for C++ to open a Socket to be able to send and receive a String.
0
santedesprit
Asked:
santedesprit
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

OpenSocket();

ConnectSocket();

ListenSocket();


0
 
keenezCommented:
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
 
TelonirCommented:
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
romanmCommented:
// 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
 
havman56Commented:
see my website i have hosted a free socket program having client and server.
it works fine.

http://micro_mangal.tripod.com
0
 
tinchosCommented:
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
 
YensidModCommented:
This question is PAQed and no points refunded (of 70)

YensidMod
Community Support Moderator
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now