接收端程序:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define myport 319
char buf[200];
int main(int argc , char *argv[])
{
int sockfd,new_sockfd;
struct sockaddr_in sin_addr,pin_addr;
int len, pin_addr_size,i;
if ((sockfd = socket(AF_INET, SOCK_STREAM,0)) <0)
{
printf("can’t create socket\n");
exit(1);
}
memset( &sin_addr, 0, sizeof(sin_addr));
sin_addr.sin_family = AF_INET;
sin_addr.sin_addr.s_addr = htonl(INADDR_ANY);
sin_addr.sin_port = htons(myport);
if (bind(sockfd, (struct sockaddr *)&sin_addr, sizeof(sin_addr))<0)
{
printf("can’t bind socket\n");
exit(1);
}
if (listen(sockfd, 5) < 0 )
{
printf("listen error\n");
exit(1);
}
while (1)
{
if ((new_sockfd = accept (sockfd, (struct sockaddr *)&pin_addr,&pin_addr_size)) < 0)
{
printf("ac