sever程序:使用的本机ip地址127.0.0.1 端口40002,可以直接使用,收啥发啥
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 40002
int main()
{
struct sockaddr_in serv_addr;
//接收客户端请求
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size = sizeof(clnt_addr);
char recvbuf[1024];
memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充
//创建套接字
int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(serv_sock == -1)
{
perror("socket err:");
exit(1);
}
//将套接字和IP、端口绑
serv_addr.sin_family = AF_INET; //使用IPv4地址
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址
serv_addr.sin_port = htons(PORT); //端口
if(bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1)
{
perror("bind err:");
exit(1);
}
//进入监听状态,等待用户发起请求
if(listen(serv_sock, 20) == -1)
{
perror("listen err");
exit(1);
}
for(;;)
{
int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
if(clnt_sock == -1)
{
perror("accept err!");
continue;
}
else
{
printf("server: got connection from %s \n",inet_ntoa(clnt_addr.sin_addr));
while(1)
{
int recv_len = recv(clnt_sock, recvbuf, 1024, 0);
if(recv_len <= 0)
{
close(clnt_sock);
return 1;
}
else
{
printf("sever recv data is:%s\n",recvbuf);
int write_len = write(clnt_sock, recvbuf, strlen(recvbuf));
if(write_len < 0)
{
close(clnt_sock);
return 1;
}
memset(recvbuf,0,strlen(recvbuf));
}
}
}
}
//关闭套接字
close(serv_sock);
return 0;
-----------------------------------------------------------------------------------------------------------------------------------------------------
client程序:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
int main(int argc,char *argv[]) {
int sockfd,numbytes;
char buf[100];
char *msg="hello world";
struct hostent *he;
struct sockaddr_in their_addr;
struct in_addr *ip_addr;
uint16_t count = 10;
ip_addr = (struct in_addr *)malloc(sizeof(struct in_addr));
memset(ip_addr,0,sizeof(struct in_addr));
//将基本名字和地址转换,用户必须输入服务器的名字为参数
if(argc<2)
{
printf("You should input IP or Name of the server!/n");
exit(1);
}
printf("%s\n",argv[1]);
unsigned long address =inet_addr(argv[1]);
if(address == INADDR_NONE)
{
printf("正在解析地址\n");
he = gethostbyname(argv[1]);
their_addr.sin_addr.s_addr = *((unsigned long *)he->h_addr_list[0]);//;inet_addr(argv[1])
printf("%d\n",their_addr.sin_addr.s_addr);
}
else
{
printf("正在解析ip\n");
if(!inet_aton(argv[1],ip_addr))
{
printf("inet_aton error/n");
return 1;
}
their_addr.sin_addr = *ip_addr;
printf("%d\n",their_addr.sin_addr.s_addr);
}
//建立一个TCP套接口
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
printf("socket ok!\n");
//初始化结构体,连接到服务器的2323端口
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(1234);
bzero(&(their_addr.sin_zero),8);
//和服务器建立连接,若连接建立失败则直接报错
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1)
{
perror("connect");
exit(1);
}
while(count--)
{
//向服务器发送字符串msg
if(send(sockfd,msg,strlen(msg),0)==-1) {
perror("send");
exit(1);
}
//接受从服务器返回的信息
if((numbytes = recv(sockfd,buf,100,0))==-1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("result:%s\n",buf);
}
close(sockfd);
return 0;
}
}