同志们,讨论的很热烈,但是没有我预期的回答,
客户端程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <signal.h>
struct test
{char *t1;
char *t2;
} cli;
int main()
{
int n, fd;
struct sockaddr_in addr;
FILE *fp;
char ipbuf[256], portbuf[256], buf[256];
fp = fopen("/etc/ip.ini", "r");
fgets(ipbuf, sizeof(ipbuf), fp);
fgets(portbuf, sizeof(portbuf), fp);
fclose(fp);
cli.t1 = "abc";
cli.t2 = "def";
addr.sin_family = AF_INET;
addr.sin_port = atoi(portbuf);
addr.sin_addr.s_addr = inet_addr(ipbuf);
fd = socket(AF_INET, SOCK_STREAM, 0);
connect(fd, (struct sockaddr *)&addr, sizeof(addr));
send(fd, &cli, sizeof(cli), 0);
//recv(fd, buf, sizeof(buf), 0);
close(fd);
exit(0);
}
服务端程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netdb.h>
struct test
{char *t1;
char *t2;
}ser;
int main()
{
int serfd, clifd;
int n, serlen, clilen;
struct sockaddr_in seraddr, cliaddr;
FILE *fp;
char ipbuf[256], portbuf[256], buf[25];
fp = fopen("/etc/ip.ini", "r");
fgets(ipbuf, sizeof(ipbuf), fp);
fgets(portbuf, sizeof(portbuf), fp);
fclose(fp);
seraddr.sin_family = AF_INET;
seraddr.sin_port = atoi(portbuf);
seraddr.sin_addr.s_addr = inet_addr(ipbuf);
serfd = socket(AF_INET, SOCK_STREAM, 0);
bind(serfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
listen(serfd, 10);
signal(SIGCHLD, SIG_IGN);
while(1){
clilen = sizeof(cliaddr);
clifd = accept(serfd, (struct sockaddr *)&cliaddr, &clilen);
if(fork() == 0) {
close(serfd);
n = recv(clifd, &ser, sizeof(struct test), 0);
//fprintf(stderr, "%d", n);
fprintf(stderr, "%s", ser.t1);
close(clifd);
exit(0);
}
else
close(clifd);
}
}
这是我在网上找的程序,但是不是c#的,大家可有灵感?