Solved

Sample codes for Socket programming

Posted on 2004-08-29
14
469 Views
Last Modified: 2013-12-14
Hi, I am currently developing a application that requires socket programming. I wonder if anyone out there could share with me some webby that provides socket programming codes for C++. I'm using visual studio 6.0 for my server application, and embedded visual C++ for my client side. I am totally new to socket programming thus I need basic explaination. Thank you.
0
Comment
Question by:mwcmp
  • 3
  • 2
  • 2
  • +4
14 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 74 total points
ID: 11925972
Hi mwcmp,
A chat application is always the simplest way to start learning socket programming, I think.
You will find many examples of socket programming with Visual C++ at codeproject.com. There is a special beginner's section:
http://www.codeproject.com/internet/


Good luck,
Jaime.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 71 total points
ID: 11926367
If you don't want to use MFC, take a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/finished_server_and_client_code.asp ("Complete Server and Client Code")
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 11926429
0
 
LVL 11

Assisted Solution

by:Jase-Coder
Jase-Coder earned 71 total points
ID: 11926440
oh, I forgot also check gametutorials.com they have a networking section in the tutorials ideal for beginners

http://gametutorials.com/Tutorials/Networking/Networking_Pg1.htm
0
 

Assisted Solution

by:sumn79
sumn79 earned 71 total points
ID: 11928247
try this: this sample just make a HTTP post to a php

#include <windows.h>
#include <winsock.h>

void main()
{
      if (!InitWinSock()) return;
      HTTPPost("192.168.1.100",80,"/testsocket.php",NULL,0,NULL)
      WSACleanup();
}

BOOL InitWinSock()
{
      WSADATA wsaData;
      int iResult=WSAStartup(MAKEWORD(2,2),&wsaData);
      if (iResult!=NO_ERROR) return FALSE;

      return TRUE;
}

BOOL HTTPPost(const char *addr,int port,char *urlMsg,char *data,int dataLen,char *retStr)
{
      if (urlMsg==NULL) return FALSE;
      if (data==NULL&&dataLen>0) return FALSE;
      
      SOCKET m_socket;
      m_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
      BOOL isSent=FALSE;
      if (m_socket!=INVALID_SOCKET)
      {
            sockaddr_in clientService;

            clientService.sin_family=AF_INET;
            clientService.sin_addr.s_addr=inet_addr(addr);
            clientService.sin_port = htons(port);
            if (connect(m_socket,(SOCKADDR*)&clientService,sizeof(clientService))!=SOCKET_ERROR)
            {
                  char sendbuf[1024];
                  wsprintf(sendbuf,"%s HTTP/1.0\r\n",urlMsg);
                  send(m_socket,sendbuf,strlen(sendbuf),0);
                  
                  wsprintf(sendbuf,"Content-type: application/x-www-form-urlencoded\r\n");
                  send(m_socket,sendbuf,strlen(sendbuf),0);
                  
                  wsprintf(sendbuf,"Content-length: %d\r\n",dataLen);
                  send(m_socket,sendbuf,strlen(sendbuf),0);
                  
                  wsprintf(sendbuf,"Connection: close\r\n\r\n");
                  send(m_socket,sendbuf,strlen(sendbuf),0);

                  if (data!=NULL)
                  {
                        send(m_socket,data,dataLen,0);
                  }
                  isSent=TRUE;
                  
                  int bytesRecv=SOCKET_ERROR;
                  char recvbuf[1024];
                  int accSize=0;
                  while( bytesRecv == SOCKET_ERROR )
                  {
                        bytesRecv = recv(m_socket,recvbuf,1024,0);
                        accSize+=bytesRecv;
                        if (bytesRecv==0||bytesRecv==WSAECONNRESET)
                        {
                              break;
                        }
                        printf("Bytes Recv: %ld\n",bytesRecv);
                  }
                  *(recvbuf+accSize)='\0';
                  if (accSize>0&&retStr!=NULL)
                  {
                        lstrcpy(retStr,recvbuf);
                  }

            }
            
            closesocket(m_socket);
      }      
      return isSent;
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Assisted Solution

by:carribus
carribus earned 71 total points
ID: 11928839
Hi there,

there are various ways to tackle socket programming, but to learn it, I can only point you in the direction that I went.
Have a look at this document called : Beej's Guide to Network Programming.

http://www.ecst.csuchico.edu/~beej/guide/net/

It is written for people who have NEVER done network development before, and therefore is easy to follow and understand.

Enjoy!
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 71 total points
ID: 11936318
Try the WinSock Programmer's FAQ: http://tangentsoft.net/wskfaq/

0
 
LVL 4

Expert Comment

by:havman56
ID: 11953114
Lot of developers used my socket code and got benefited

u can go and find it in my web site

http://micro_mangal.tripod.com

it is in free code section

0
 
LVL 4

Assisted Solution

by:havman56
havman56 earned 71 total points
ID: 11953149
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 12310444
I think i made comments, so I should get at least a share of the points
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12328005
All we have provided samples for socket programming.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now