Solved

Sample codes for Socket programming

Posted on 2004-08-29
14
481 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
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 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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

679 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