📄 client.c
字号:
/*********************************************************************
* File: client.c
* Author: Embest J.Zhao 2004.12.10
* Desc: client
* History:
*********************************************************************/
#include<sys/socket.h>
#include<stdio.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define MYPORT 8000
//local host
char * host_name="127.0.0.1";
main(int argc,char *argv[]) {
int sockfd,con_fd,numbytes,ret,pid;
struct sockaddr_in server_addr;
struct hostent *their_addr;
int sin_size;
int lis;
char buf[256];
char *str="Default test string.\n";
char *ip="192.192.192.12\n";
//if you input the new host ip after the command
//then change the default local host ip address.
if (argc<2)
printf("default local host : 192.192.192.12 , or you can input one after the command.\n");
else {
ip=argv[1];
printf("local host ip = %s\n",ip);
}
socketing:
//creat socket.
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(MYPORT);
server_addr.sin_addr.s_addr=inet_addr(ip); //inet_addr(ip);
bzero(&(server_addr.sin_zero),8);
sockfd=socket(AF_INET,SOCK_STREAM,0);
if (sockfd==-10) {
printf("failed when creating\n");
exit(-1);
}
//connect the server.
if (connect(sockfd,(void*)&server_addr,sizeof(server_addr))==-1) {
printf("failed when connecting\n");
exit(-1);
}
//send the default string to server.
printf(" send to server...%s\n",str);
if (send(sockfd,str,strlen(str),0)==-1) {
printf("failed when sending\n");
exit(-1);
}
//receive a string from server.
if (recv(sockfd,buf,sizeof(buf),0)==-1) {
printf("failed when receiving\n");
exit(-1);
}
printf(" response from server...%s\n",buf);
close(sockfd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -