⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.c

📁 socket, server.c and client.c
💻 C
字号:
/**************************************************************** * $ID: client.c        三, 26  3月 2008 15:36:59 +0800  root $ * *                                                              * * Description:                                                 * *                                                              * * Maintainer:  范美辉(Meihui Fan)  <mhfan@hhcn.com>            * *                                                              * * Copyright (C)  2008  HHTech                                  * *   www.hhcn.com, www.hhcn.org                                 * *   All rights reserved.                                       * *                                                              * * This file is free software;                                  * *   you are free to modify and/or redistribute it   	        * *   under the terms of the GNU General Public Licence (GPL).   * *                                                              * * Last modified: 三, 26  3月 2008 17:01:07 +0800       by root # ****************************************************************/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#define PORT	4000#define BACKLOG	10int main(int argc, char *argv[]){    int sock_fd, new_fd;    int sin_size;    struct sockaddr_in server_addr;    struct hostent *he;    char welcome[50];    char buf[100];        if(argc != 2)    {	printf("USAGE: client <server>\n");	exit(1);    }    if((he = gethostbyname(argv[1])) == NULL)    {	printf("gethostbyname");	exit(1);    }    if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)    {	perror("socket");	exit(1);    }    server_addr.sin_family = AF_INET;    server_addr.sin_port = htons(PORT);    server_addr.sin_addr = *((struct in_addr *)(he->h_addr));    bzero(&(server_addr.sin_zero), 8);    if(connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)    {	perror("connect");	close(sock_fd);	exit(1);    }    if(recv(sock_fd, &welcome[0], 50, 0) == -1)    {	perror("recv");	close(sock_fd);	exit(1);    }    printf(welcome);    while(1)    {	printf("send => ");	while(fgets(buf, 100, stdin) == NULL);	send(sock_fd, &buf[0], 100, 0);	recv(sock_fd, &buf[0], 100, 0);	printf("receive => %s\n", &buf[0]);    }    close(sock_fd);    return 0;}/******************** End Of File: client.c ********************/// vim:sts=4:ts=8: 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -