• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

Sample codes for Socket programming

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
mwcmp
Asked:
mwcmp
  • 3
  • 2
  • 2
  • +4
7 Solutions
 
Jaime OlivaresCommented:
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
 
jkrCommented:
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
 
Jase-CoderCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Jase-CoderCommented:
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
 
sumn79Commented:
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
 
carribusCommented:
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
 
waysideCommented:
Try the WinSock Programmer's FAQ: http://tangentsoft.net/wskfaq/

0
 
havman56Commented:
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
 
Jase-CoderCommented:
I think i made comments, so I should get at least a share of the points
0
 
Jaime OlivaresCommented:
All we have provided samples for socket programming.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now