Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VS 2010 Windows Socket Programming---MultiClient Ask for Help

8 views
Skip to first unread message

iamd...@gmail.com

unread,
Nov 15, 2013, 3:47:03 AM11/15/13
to
#include <winsock.h>
#pragma comment(lib, "wsock32.lib")

#include <stdio.h>
#include <string.h>

#define SERVER_PORT 5432
#define MAX_BUFSIZE 256

int main(int argc, char * argv[])
{
/* wliu comments: required for windows socket programming */
WSADATA WSAData;
int WSAreturn;

/* wliu comments: useless pointer */
//FILE *fp;

struct hostent *hp;
struct sockaddr_in sin;
char *host;
char bufIn[MAX_BUFSIZE],bufOut[MAX_BUFSIZE];
int s, new_s;
int len;
int ableToSendMsg = 1;

host = "localhost";
/* wliu comments: modified for windows socket programming */
WSAreturn = WSAStartup(0x101,&WSAData);
if(WSAreturn)
{
fprintf(stderr, "duplex-talk: WSA error.\n");
exit(1);
}

/* translate host name into peer's IP address */
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "duplex-talk: unknown host: %s\n", host);
exit(1);
}



/* build address data structure */
memset((char *)&sin, 0, sizeof(sin));
memcpy((char *)&sin.sin_addr, hp->h_addr, hp->h_length);

sin.sin_family = AF_INET;
sin.sin_port = htons(SERVER_PORT);

/* active open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("duplex-talk: socket failed.");
exit(1);
}

if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("duplex-talk: connect failed.");
/* wliu comments: modified for windows socket programming */
//close(s);
closesocket(s);
exit(1);
}

/* wliu comments: displaying current status */
printf("[duplex-talk client] connection to %s is ready\n", host);
printf("[duplex-talk client] please input your message (empty input to halt):\n");

/* wliu comments: modification to support connection termination */

/* main loop: get and send lines of text */



while (1) {
if(ableToSendMsg){
//Send Messgage
fgets(bufIn, sizeof(bufIn), stdin);

/* wliu comments: modified to stop sending */
if ( strlen(bufIn) == 1 ) {
/* wliu comments: user input empty message with '\n' */
bufIn[0] = '\0';
send(s, bufIn, 1, 0);
printf("[duplex-talk client] empty message is send to server\n");
break;
} else {
bufIn[MAX_BUFSIZE-1] = '\0';
len = strlen(bufIn) + 1;
send(s, bufIn, len, 0);
printf("[duplexx-talk client] send %d chars to server\n\n", strlen(bufIn));
ableToSendMsg = 0;
}

}
else{
// Receive Message
len = recv(s, bufOut, sizeof(bufOut), 0);

// /* wliu comments: modified to stop sending */
if ( strlen(bufOut) == 0 ) {
/* wliu comments: received empty message */
printf("[duplex-talk client] empty message is received\n");
break;
} else {
printf("[duplex-talk client] received %d chars \n", strlen(bufOut));
fputs(bufOut, stdout);
printf("\n");
ableToSendMsg = 1;
}

}

}
printf("[duplex-talk client] connection is terminated\n");

/* wliu comments: modified for windows socket programming */
WSACleanup();
return 1;
}

Yang Du

unread,
Nov 15, 2013, 3:49:42 AM11/15/13
to
在 2013年11月15日星期五UTC+8下午4时47分03秒,Yang Du写道:
>I am a junior student for Wuhan,Hubei,China....This is one of my Computer Networks Homework.And I cannot get it done. I hope you can help me.Many thanks!
0 new messages