Click here to Skip to main content
15,943,406 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Second Window in Win32 Pin
enhzflep5-Apr-10 8:36
enhzflep5-Apr-10 8:36 
QuestionRe: Second Window in Win32 Pin
Fareed Rizkalla5-Apr-10 15:41
Fareed Rizkalla5-Apr-10 15:41 
AnswerRe: Second Window in Win32 Pin
enhzflep6-Apr-10 2:12
enhzflep6-Apr-10 2:12 
GeneralRe: Second Window in Win32 Pin
Fareed Rizkalla6-Apr-10 5:13
Fareed Rizkalla6-Apr-10 5:13 
GeneralRe: Second Window in Win32 Pin
enhzflep6-Apr-10 17:42
enhzflep6-Apr-10 17:42 
GeneralRe: Second Window in Win32 Pin
Fareed Rizkalla5-Apr-10 16:28
Fareed Rizkalla5-Apr-10 16:28 
AnswerRe: Second Window in Win32 Pin
Garth J Lancaster4-Apr-10 15:11
professionalGarth J Lancaster4-Apr-10 15:11 
Questionsocket programming in c help Pin
kedah4-Apr-10 8:48
kedah4-Apr-10 8:48 
i'm doing a simple program in socket programming on c i have server that can handle 2clients in a single machine i'm running ubuntu linux so i got it work but the probelm when clients send a message the server will echo it but i cant distinguish which client send the message client 1 or client 2 here is the code for the server whenever a client sent a message it will be shown in the server side as( client sent : hi)i cant know which client sent it please help


 #include <stdio.h><br />
#include <stdlib.h><br />
#include <string.h><br />
#include <arpa/inet.h><br />
#include <sys/types.h><br />
#include <sys/socket.h><br />
 <br />
#define BUFFER_SIZE  1024<br />
 <br />
int serverfd;<br />
int clientfd;<br />
 <br />
int n;<br />
int addrSize;<br />
int serverPort;<br />
char clientIP[25];<br />
char buffer[BUFFER_SIZE];<br />
struct sockaddr_in serverAddr;<br />
struct sockaddr_in clientAddr;<br />
 <br />
int main(int argc, char* argv[])<br />
{<br />
    if(argc < 2)<br />
    {<br />
        printf("Usage: ./server <port>\n");<br />
        exit(0);<br />
    }<br />
    else<br />
    {<br />
        serverPort = atoi(argv[1]);<br />
    }<br />
 <br />
    addrSize = sizeof(struct sockaddr);<br />
 <br />
    /* Create a TCP socket */<br />
    serverfd = socket(AF_INET, SOCK_STREAM, 0);<br />
 <br />
    if(serverfd < 0)<br />
        printf("Error creating server socket");<br />
    serverAddr.sin_family = AF_INET; <br />
    serverAddr.sin_port = htons(serverPort); /* Specify a port */<br />
    serverAddr.sin_addr.s_addr = INADDR_ANY; /* Accept connections from anywhere */<br />
    memset(&(serverAddr.sin_zero), '\0', 8); /* This must be set to zero */<br />
 <br />
    /* Bind our server socket */<br />
    if(bind(serverfd, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)) == -1)<br />
        printf("Could not bind()\n");<br />
 <br />
    /* Start listening on socket for incoming connections */<br />
    if(listen(serverfd, 10) == -1)<br />
        printf("Could not listen()\n");<br />
    fork();//this is the magical line...<br />
    /* accept() accepts an incoming connection and fills a sockaddr_in structure with client information */<br />
    clientfd = accept(serverfd, (struct sockaddr*)&clientAddr, &addrSize);<br />
    <br />
    /* inet_ntoa() converts an IP address to a displayable string */<br />
    snprintf(clientIP, sizeof(clientIP), "%s", inet_ntoa(clientAddr.sin_addr));<br />
    printf("Got connection from %s\n", clientIP);<br />
 <br />
    do<br />
    {<br />
        n = recv(clientfd, buffer, BUFFER_SIZE, 0); /* Receive message from client */<br />
        buffer[n] = '\0'; /* terminate string */<br />
 <br />
        send(clientfd, buffer, BUFFER_SIZE, 0); /* Echo message back to client */<br />
        printf("Client sent: %s\n" , buffer);<br />
    }<br />
    while(strncmp(buffer, "exit", 4) != 0); /* Exit when client sends "exit" */<br />
 <br />
    close(serverfd); /* Close socket */ <br />
    <br />
    return 0;<br />
}



here is the client.c code


#include <stdio.h><br />
#include <stdlib.h><br />
#include <string.h><br />
#include<br />
<arpa/inet.h><br />
#include<br />
<sys/types.h><br />
#include<br />
<sys/socket.h><br />
<br />
#define<br />
BUFFER_SIZE  1024<br />
<br />
int sockfd;<br />
<br />
int n;<br />
int addrSize;<br />
int serverPort;<br />
char serverIP[25];<br />
char<br />
buffer[BUFFER_SIZE];<br />
struct sockaddr_in serverAddr;<br />
<br />
int main(int argc, char* argv[])<br />
{<br />
    if(argc < 3)<br />
    {<br />
        printf("Usage: ./client <server ip> <server port>\n");<br />
        exit(0);<br />
    }<br />
    else<br />
    {<br />
        snprintf(serverIP, sizeof(serverIP), "%s", argv[1]);<br />
        serverPort = atoi(argv[2]);<br />
    }<br />
<br />
    addrSize = sizeof(struct sockaddr);<br />
<br />
      /* Create a TCP socket */<br />
    sockfd = socket(AF_INET, SOCK_STREAM, 0);<br />
<br />
    if(sockfd < 0)<br />
        printf("Error creating socket");<br />
<br />
    serverAddr.sin_family = AF_INET;<br />
    serverAddr.sin_port = htons(serverPort); /* Server port */<br />
    inet_pton(AF_INET, serverIP, &serverAddr.sin_addr); /* Server IP Address */<br />
    memset(&(serverAddr.sin_zero), '\0', 8); /* This must be set to zero */<br />
<br />
      /* Connect to server */<br />
    if(connect(sockfd, (struct sockaddr*)&serverAddr, addrSize) != 0)<br />
    {<br />
        printf("Error: Could not connect to %s on port %d\n", serverIP, serverPort);<br />
        exit(0);<br />
    }<br />
    else<br />
        printf("Connected to %s on port %d\n\n", serverIP, serverPort);<br />
<br />
    do<br />
    {<br />
        printf("> ");<br />
        fgets(buffer, BUFFER_SIZE, stdin);<br />
        send(sockfd, buffer, strlen(buffer), 0); /* Send message to server */<br />
<br />
        n = recv(sockfd, buffer, BUFFER_SIZE, 0); /* Receive message from server */<br />
        buffer[n] = '\0'; /* terminate string */<br />
        printf("< %s\n", buffer);<br />
    }<br />
    while(strncmp(buffer, "exit", 4) != 0); /* Exit when message is "exit" */<br />
<br />
    close(sockfd); /* Close socket */<br />
<br />
    return 0;<br />
}

AnswerRe: socket programming in c help Pin
Richard Andrew x644-Apr-10 11:49
professionalRichard Andrew x644-Apr-10 11:49 
AnswerRe: socket programming in c help Pin
Garth J Lancaster4-Apr-10 12:29
professionalGarth J Lancaster4-Apr-10 12:29 
QuestionIs the handle from OpenProcess Unique Pin
ForNow4-Apr-10 7:37
ForNow4-Apr-10 7:37 
QuestionRecieving device events in a service Pin
Ap0ll0-134-Apr-10 3:42
Ap0ll0-134-Apr-10 3:42 
QuestionHow to get the net speed of your computer and how to get the current webpage's speed? Pin
Aric Wang4-Apr-10 0:42
Aric Wang4-Apr-10 0:42 
QuestionRe: How to get the net speed of your computer and how to get the current webpage's speed? Pin
enhzflep4-Apr-10 3:31
enhzflep4-Apr-10 3:31 
QuestionApplication crash - on use of IXMLHTTPRequest send method Pin
dinesh babu3-Apr-10 20:47
dinesh babu3-Apr-10 20:47 
QuestionIs my merge list right? Pin
wbgxx3-Apr-10 18:36
wbgxx3-Apr-10 18:36 
AnswerRe: Is my merge list right? Pin
Luc Pattyn3-Apr-10 21:06
sitebuilderLuc Pattyn3-Apr-10 21:06 
QuestionCWinApp <-> DLL communication Pin
hxhl953-Apr-10 12:09
hxhl953-Apr-10 12:09 
QuestionRe: CWinApp DLL communication Pin
Tim Craig3-Apr-10 13:55
Tim Craig3-Apr-10 13:55 
AnswerRe: CWinApp DLL communication Pin
hxhl953-Apr-10 15:56
hxhl953-Apr-10 15:56 
GeneralRe: CWinApp DLL communication Pin
Tim Craig3-Apr-10 16:07
Tim Craig3-Apr-10 16:07 
GeneralRe: CWinApp DLL communication Pin
hxhl953-Apr-10 16:10
hxhl953-Apr-10 16:10 
QuestionIE8 and ShellExecute weirdness Pin
Monty23-Apr-10 9:41
Monty23-Apr-10 9:41 
AnswerRe: IE8 and ShellExecute weirdness Pin
LunaticFringe3-Apr-10 12:37
LunaticFringe3-Apr-10 12:37 
GeneralRe: IE8 and ShellExecute weirdness Pin
Luc Pattyn3-Apr-10 13:41
sitebuilderLuc Pattyn3-Apr-10 13:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.