#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IP “127.0.0.1”
#define PORT 7788
#define MAXLEN 1024
int main(int argc,char* argv[])
{
int sock_fd = 0;
int iret = 0;
struct sockaddr_in sock_addr;
char buf[MAXLEN] = {0};
//创建socket
if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
perror(“socket error”);
exit(1);
}
//发送连接服务器的请求
memset(&sock_addr,0,sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = htons(PORT);
sock_addr.sin_addr.s_addr = inet_addr(IP);
if(connect(sock_fd,(struct sockaddr*)(&sock_addr),sizeof(sock_addr))==-1)
{
perror(“connect error”);
exit(1);
}
printf(“connect success!\n”);
while(1) //发送数据
{
memset(buf,’\0′,sizeof(buf));
gets(buf);
iret=write(sock_fd,buf,strlen(buf));
printf(“write data:%s,len=%d\n”,buf,iret);
if(strcmp(buf,”quit”) == 0)
{
printf(“即将退出 …\n”);
sleep(3);
break;
}
}
close(sock_fd); //关闭套接字
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#define IP “127.0.0.1”
#define PORT 7788
#define MAXLEN 1024
int main(int argc,char* argv[])
{
int sock_fd = 0;
int iret = 0;
struct sockaddr_in sock_addr;
char buf[MAXLEN] = {0};
//创建socket
if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
perror(“socket error”);
exit(1);
}
//发送连接服务器的请求
memset(&sock_addr,0,sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = htons(PORT);
sock_addr.sin_addr.s_addr = inet_addr(IP);
if(connect(sock_fd,(struct sockaddr*)(&sock_addr),sizeof(sock_addr))==-1)
{
perror(“connect error”);
exit(1);
}
printf(“connect success!\n”);
while(1) //发送数据
{
memset(buf,’\0′,sizeof(buf));
gets(buf);
iret=write(sock_fd,buf,strlen(buf));
printf(“write data:%s,len=%d\n”,buf,iret);
if(strcmp(buf,”quit”) == 0)
{
printf(“即将退出 …\n”);
sleep(3);
break;
}
}
close(sock_fd); //关闭套接字
return 0;
}