Thread: Code for Client/Server Socket

  1. #1
    Unregistered
    Guest

    Question Code for Client/Server Socket

    Hi,
    I want the code for Client and Server Socket to send a text message from one computer to another. I used the help file, but it dosen't provide enough information. I am doing this program in C++ Builder.

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    Here's a minimal example

    /* CLIENT */
    #include <winsock.h>
    #include <stdio.h>

    int main( int argc, char** argv){
    char pBuf[2048];
    WSADATA wsaData;
    SOCKET sClient;
    int connected;
    SOCKADDR_IN RemoteAddr; RemoteAddr.sin_family=PF_INET;
    RemoteAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    RemoteAddr.sin_port=htons(5555);
    WSAStartup(MAKEWORD(1, 1), &wsaData);
    sClient=socket(AF_INET, SOCK_STREAM, 0);
    connected = 1;
    while(connected){
    connected = connect(sClient, (PSOCKADDR)&RemoteAddr, sizeof(SOCKADDR));
    if(!connected){
    strcpy(pBuf,"");
    while(strcmp(pBuf,"end")){
    scanf("%s",&pBuf);
    send(sClient, pBuf, strlen(pBuf)+1, 0);}}}
    closesocket(sClient);
    WSACleanup();
    return 0;}


    /* SERVER */
    #include <winsock.h>
    #include <stdio.h>

    int main(int argc, char** argv) {
    char pBuf[2048];
    WSADATA wsaData; WORD version;
    SOCKET listeningSocket, theClient;
    SOCKADDR_IN saServer; saServer.sin_family = AF_INET;
    saServer.sin_addr.s_addr = INADDR_ANY;
    saServer.sin_port = htons(5555);
    version = MAKEWORD(1,1);
    WSAStartup(version, &wsaData);
    listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    bind(listeningSocket, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
    listen(listeningSocket, 10);
    theClient=INVALID_SOCKET;
    while(1){
    theClient = accept(listeningSocket,NULL, NULL);
    if(theClient!=INVALID_SOCKET)
    while(strcmp(pBuf,"end")){
    recv(theClient, pBuf, 2048, 0);
    printf( "-->%s\n", pBuf);}
    strcpy(pBuf,"");
    theClient=INVALID_SOCKET;}
    closesocket(theClient);
    closesocket(listeningSocket);
    WSACleanup();
    return 0;}

  3. #3
    Unregistered
    Guest

    Question

    Thaks for your code, it actually works, but I have a question? Where do you put the code?

    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    What is your purpose ?
    Is it a windowed app?

  5. #5
    Unregistered
    Guest

    Question

    It's a school project that I have to send a message from one computer to another using the same program like a chat line.

    THank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help understanding a code
    By lolguy in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-15-2009, 04:13 PM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Socket Programming Introduction Source Code
    By Lynux-Penguin in forum Linux Programming
    Replies: 2
    Last Post: 04-30-2002, 05:29 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM