7 个解决方案
#1
顶顶
#2
贴代码
#3
高手帮忙啊
#4
//这是服务器端的程序
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#define SERVPORT 3334
#define BACKLOG 10
#define MAXDATASIZE 100
#define SERVPORTT 3333
#define HOSTADDS "127.0.0.1"
int client_fd1;
int client_fd2;
void send_function();
void send1_function();
void recv_function();
main() {
pthread_t thread1,thread2;
int one = 1;
int sockfd1,sockfd2;
int sin_size;
struct sockaddr_in my_addr1,my_addr2;
struct sockaddr_in remote_addr1,remote_addr2;
sockfd1 = socket(AF_INET,SOCK_STREAM,0);
sockfd2 = socket(AF_INET,SOCK_STREAM,0);
if(sockfd1 == -1 || sockfd2 == -1) {
perror("socket创建出错!");
exit(1);
}
memset(&my_addr1,0,sizeof(my_addr1));
my_addr1.sin_family=AF_INET;
my_addr1.sin_port=htons(SERVPORT);
my_addr1.sin_addr.s_addr = inet_addr(HOSTADDS);
memset(&my_addr2,0,sizeof(my_addr2));
my_addr2.sin_family=AF_INET;
my_addr2.sin_port=htons(SERVPORTT);
my_addr2.sin_addr.s_addr = inet_addr(HOSTADDS);
//为了重复绑定IP地址,必须对套接字进行设置
if(setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
if(setsockopt(sockfd2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
//绑定
if(bind(sockfd1,(struct sockaddr *)&my_addr1,sizeof(my_addr1)))
{
printf("发送套接字绑定失败");
exit(1);
}
if(bind(sockfd2,(struct sockaddr *)&my_addr2,sizeof(my_addr2)))
{
printf("接收套接字绑定失败");
exit(1);
}
//侦听
if(listen(sockfd1,BACKLOG) == -1 || listen(sockfd2,BACKLOG)== -1) {
perror("listen出错!");
exit(1);
}
while(1) {
sin_size = sizeof (struct sockaddr_in);
if((client_fd1 = accept(sockfd1,(struct sockaddr *)&remote_addr1,&sin_size)) == -1 ) {
perror("accept出错");
continue;
}
if((client_fd2 = accept(sockfd2,(struct sockaddr *)&remote_addr2,&sin_size)) == -1 ) {
perror("accept出错");
continue;
}
}
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2,NULL,(void *)send1_function,NULL);
sleep(10);
pthread_create(&thread1,NULL,(void *)send_function,NULL);
sleep(10);
}
void send_function() {
if(send (client_fd1,"Hello ,you are connected!\n",26,0) == -1)
perror("send 出错!");
else{
close(client_fd1);
exit(0);
}
}
void send1_function() {
if(send (client_fd2,"Hello ,you are connected!\n",26,0) == -1)
perror("send 出错!");
else{
close(client_fd2);
exit(0);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#define SERVPORT 3334
#define BACKLOG 10
#define MAXDATASIZE 100
#define SERVPORTT 3333
#define HOSTADDS "127.0.0.1"
int client_fd1;
int client_fd2;
void send_function();
void send1_function();
void recv_function();
main() {
pthread_t thread1,thread2;
int one = 1;
int sockfd1,sockfd2;
int sin_size;
struct sockaddr_in my_addr1,my_addr2;
struct sockaddr_in remote_addr1,remote_addr2;
sockfd1 = socket(AF_INET,SOCK_STREAM,0);
sockfd2 = socket(AF_INET,SOCK_STREAM,0);
if(sockfd1 == -1 || sockfd2 == -1) {
perror("socket创建出错!");
exit(1);
}
memset(&my_addr1,0,sizeof(my_addr1));
my_addr1.sin_family=AF_INET;
my_addr1.sin_port=htons(SERVPORT);
my_addr1.sin_addr.s_addr = inet_addr(HOSTADDS);
memset(&my_addr2,0,sizeof(my_addr2));
my_addr2.sin_family=AF_INET;
my_addr2.sin_port=htons(SERVPORTT);
my_addr2.sin_addr.s_addr = inet_addr(HOSTADDS);
//为了重复绑定IP地址,必须对套接字进行设置
if(setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
if(setsockopt(sockfd2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
//绑定
if(bind(sockfd1,(struct sockaddr *)&my_addr1,sizeof(my_addr1)))
{
printf("发送套接字绑定失败");
exit(1);
}
if(bind(sockfd2,(struct sockaddr *)&my_addr2,sizeof(my_addr2)))
{
printf("接收套接字绑定失败");
exit(1);
}
//侦听
if(listen(sockfd1,BACKLOG) == -1 || listen(sockfd2,BACKLOG)== -1) {
perror("listen出错!");
exit(1);
}
while(1) {
sin_size = sizeof (struct sockaddr_in);
if((client_fd1 = accept(sockfd1,(struct sockaddr *)&remote_addr1,&sin_size)) == -1 ) {
perror("accept出错");
continue;
}
if((client_fd2 = accept(sockfd2,(struct sockaddr *)&remote_addr2,&sin_size)) == -1 ) {
perror("accept出错");
continue;
}
}
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2,NULL,(void *)send1_function,NULL);
sleep(10);
pthread_create(&thread1,NULL,(void *)send_function,NULL);
sleep(10);
}
void send_function() {
if(send (client_fd1,"Hello ,you are connected!\n",26,0) == -1)
perror("send 出错!");
else{
close(client_fd1);
exit(0);
}
}
void send1_function() {
if(send (client_fd2,"Hello ,you are connected!\n",26,0) == -1)
perror("send 出错!");
else{
close(client_fd2);
exit(0);
}
}
#5
//这是客户端的
#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 <pthread.h>
#define SERVPORTT 3333
#define SERVPORT 3334
#define MAXDATASIZE 100
#define SERVER_HOST_ADDR "127.0.0.1"
void recv_function();//从3334端口接受数据
void recv1_function();//从3333端口接受数据
main(int argc, char **argv) {
if (argc != 2)
{
printf("Usage: ./%s ServerIPAddress\n",argv[0]);
exit(1);
}
pthread_t thread1,thread2;
pthread_create(&thread1,NULL,(void *)recv1_function,NULL);
sleep(10);
pthread_create(&thread2,NULL,(void *)recv_function,NULL);
sleep(10);
}
void recv_function() {
int sockfd,recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port = htons(SERVPORT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv出错了!");
exit(1);
}
buf[recvbytes]='\0';
printf("Received : %s,%d",buf,SERVPORT);
}
void recv1_function() {
int sockfd,recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port = htons(SERVPORTT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv出错了!");
exit(1);
}
buf[recvbytes]='\0';
printf("Received : %s,%d",buf,SERVPORTT);
}
#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 <pthread.h>
#define SERVPORTT 3333
#define SERVPORT 3334
#define MAXDATASIZE 100
#define SERVER_HOST_ADDR "127.0.0.1"
void recv_function();//从3334端口接受数据
void recv1_function();//从3333端口接受数据
main(int argc, char **argv) {
if (argc != 2)
{
printf("Usage: ./%s ServerIPAddress\n",argv[0]);
exit(1);
}
pthread_t thread1,thread2;
pthread_create(&thread1,NULL,(void *)recv1_function,NULL);
sleep(10);
pthread_create(&thread2,NULL,(void *)recv_function,NULL);
sleep(10);
}
void recv_function() {
int sockfd,recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port = htons(SERVPORT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv出错了!");
exit(1);
}
buf[recvbytes]='\0';
printf("Received : %s,%d",buf,SERVPORT);
}
void recv1_function() {
int sockfd,recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port = htons(SERVPORTT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv出错了!");
exit(1);
}
buf[recvbytes]='\0';
printf("Received : %s,%d",buf,SERVPORTT);
}
#6
把你的程序重新整理了一下,有点反应了:
//这是客户端的
#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 <pthread.h>
#define SERVPORTT 3333
#define SERVPORT 3334
#define MAXDATASIZE 100
//#define SERVER_HOST_ADDR "127.0.0.1"
//void recv_function(); //从3334端口接受数据
//void recv1_function(); //从3333端口接受数据
void recv_function(char *server_addr); //从3334端口接受数据
void recv1_function(char *server_addr); //从3333端口接受数据
main(int argc, char **argv)
{
if (argc != 2) {
printf("Usage: ./%s ServerIPAddress\n", argv[0]);
exit(1);
}
pthread_t thread1, thread2;
// pthread_create(&thread1, NULL, (void *) recv1_function, NULL);
// sleep(10);
// pthread_create(&thread2, NULL, (void *) recv_function, NULL);
// sleep(10);
pthread_create(&thread1, NULL, (void *) recv1_function, argv[1]);
sleep(5);
pthread_create(&thread2, NULL, (void *) recv_function, argv[1]);
sleep(5);
}
//void recv_function()
void recv_function(char *server_addr)
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
// serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
serv_addr.sin_addr.s_addr = inet_addr(server_addr);
bzero(&(serv_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
== -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv出错了!");
exit(1);
}
buf[recvbytes] = '\0';
// printf("Received : %s,%d", buf, SERVPORT);
printf("Received : %s,%d\n", buf, SERVPORT);
}
//void recv1_function()
void recv1_function(char *server_addr)
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORTT);
// serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
serv_addr.sin_addr.s_addr = inet_addr(server_addr);
bzero(&(serv_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
== -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv出错了!");
exit(1);
}
buf[recvbytes] = '\0';
// printf("Received : %s,%d", buf, SERVPORTT);
printf("Received : %s,%d\n", buf, SERVPORTT);
}
//这是客户端的
#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 <pthread.h>
#define SERVPORTT 3333
#define SERVPORT 3334
#define MAXDATASIZE 100
//#define SERVER_HOST_ADDR "127.0.0.1"
//void recv_function(); //从3334端口接受数据
//void recv1_function(); //从3333端口接受数据
void recv_function(char *server_addr); //从3334端口接受数据
void recv1_function(char *server_addr); //从3333端口接受数据
main(int argc, char **argv)
{
if (argc != 2) {
printf("Usage: ./%s ServerIPAddress\n", argv[0]);
exit(1);
}
pthread_t thread1, thread2;
// pthread_create(&thread1, NULL, (void *) recv1_function, NULL);
// sleep(10);
// pthread_create(&thread2, NULL, (void *) recv_function, NULL);
// sleep(10);
pthread_create(&thread1, NULL, (void *) recv1_function, argv[1]);
sleep(5);
pthread_create(&thread2, NULL, (void *) recv_function, argv[1]);
sleep(5);
}
//void recv_function()
void recv_function(char *server_addr)
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
// serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
serv_addr.sin_addr.s_addr = inet_addr(server_addr);
bzero(&(serv_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
== -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv出错了!");
exit(1);
}
buf[recvbytes] = '\0';
// printf("Received : %s,%d", buf, SERVPORT);
printf("Received : %s,%d\n", buf, SERVPORT);
}
//void recv1_function()
void recv1_function(char *server_addr)
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORTT);
// serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
serv_addr.sin_addr.s_addr = inet_addr(server_addr);
bzero(&(serv_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
== -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv出错了!");
exit(1);
}
buf[recvbytes] = '\0';
// printf("Received : %s,%d", buf, SERVPORTT);
printf("Received : %s,%d\n", buf, SERVPORTT);
}
#7
服务器端:
//这是服务器端的程序
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#define SERVPORT 3334
#define BACKLOG 10
#define MAXDATASIZE 100
#define SERVPORTT 3333
//#define HOSTADDS "127.0.0.1"
int client_fd1;
int client_fd2;
void send_function();
void send1_function();
void recv_function();
main()
{
pthread_t thread1, thread2;
int one = 1;
int sockfd1, sockfd2;
int sin_size;
struct sockaddr_in my_addr1, my_addr2;
struct sockaddr_in remote_addr1, remote_addr2;
sockfd1 = socket(AF_INET, SOCK_STREAM, 0);
sockfd2 = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd1 == -1 || sockfd2 == -1) {
perror("socket创建出错!");
exit(1);
}
memset(&my_addr1, 0, sizeof(my_addr1));
my_addr1.sin_family = AF_INET;
my_addr1.sin_port = htons(SERVPORT);
my_addr1.sin_addr.s_addr = htons(INADDR_ANY);
memset(&my_addr2, 0, sizeof(my_addr2));
my_addr2.sin_family = AF_INET;
my_addr2.sin_port = htons(SERVPORTT);
my_addr2.sin_addr.s_addr = htons(INADDR_ANY);
//为了重复绑定IP地址,必须对套接字进行设置
if (setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) ==
-1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
if (setsockopt(sockfd2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) ==
-1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
//绑定
if (bind(sockfd1, (struct sockaddr *) &my_addr1, sizeof(my_addr1))) {
printf("发送套接字绑定失败");
exit(1);
}
if (bind(sockfd2, (struct sockaddr *) &my_addr2, sizeof(my_addr2))) {
printf("接收套接字绑定失败");
exit(1);
}
//侦听
if (listen(sockfd1, BACKLOG) == -1 || listen(sockfd2, BACKLOG) == -1) {
perror("listen出错!");
exit(1);
}
while (1) {
sin_size = sizeof(struct sockaddr_in);
if ((client_fd1 =
accept(sockfd1, (struct sockaddr *) &remote_addr1,
&sin_size)) == -1) {
perror("accept出错");
continue;
}
if ((client_fd2 =
accept(sockfd2, (struct sockaddr *) &remote_addr2,
&sin_size)) == -1) {
perror("accept出错");
continue;
}
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2, NULL, (void *) send1_function, NULL);
sleep(1);
pthread_create(&thread1, NULL, (void *) send_function, NULL);
sleep(1);
}
/*
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2, NULL, (void *) send1_function, NULL);
sleep(1);
pthread_create(&thread1, NULL, (void *) send_function, NULL);
sleep(1);
*/
}
void send_function()
{
if (send(client_fd1, "Hello ,you are connected!\n", 26, 0) == -1)
perror("send 出错!");
else {
close(client_fd1);
//exit(0);
}
}
void send1_function()
{
if (send(client_fd2, "Hello ,you are connected!\n", 26, 0) == -1)
perror("send 出错!");
else {
close(client_fd2);
//exit(0);
}
}
//这是服务器端的程序
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#define SERVPORT 3334
#define BACKLOG 10
#define MAXDATASIZE 100
#define SERVPORTT 3333
//#define HOSTADDS "127.0.0.1"
int client_fd1;
int client_fd2;
void send_function();
void send1_function();
void recv_function();
main()
{
pthread_t thread1, thread2;
int one = 1;
int sockfd1, sockfd2;
int sin_size;
struct sockaddr_in my_addr1, my_addr2;
struct sockaddr_in remote_addr1, remote_addr2;
sockfd1 = socket(AF_INET, SOCK_STREAM, 0);
sockfd2 = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd1 == -1 || sockfd2 == -1) {
perror("socket创建出错!");
exit(1);
}
memset(&my_addr1, 0, sizeof(my_addr1));
my_addr1.sin_family = AF_INET;
my_addr1.sin_port = htons(SERVPORT);
my_addr1.sin_addr.s_addr = htons(INADDR_ANY);
memset(&my_addr2, 0, sizeof(my_addr2));
my_addr2.sin_family = AF_INET;
my_addr2.sin_port = htons(SERVPORTT);
my_addr2.sin_addr.s_addr = htons(INADDR_ANY);
//为了重复绑定IP地址,必须对套接字进行设置
if (setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) ==
-1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
if (setsockopt(sockfd2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) ==
-1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
//绑定
if (bind(sockfd1, (struct sockaddr *) &my_addr1, sizeof(my_addr1))) {
printf("发送套接字绑定失败");
exit(1);
}
if (bind(sockfd2, (struct sockaddr *) &my_addr2, sizeof(my_addr2))) {
printf("接收套接字绑定失败");
exit(1);
}
//侦听
if (listen(sockfd1, BACKLOG) == -1 || listen(sockfd2, BACKLOG) == -1) {
perror("listen出错!");
exit(1);
}
while (1) {
sin_size = sizeof(struct sockaddr_in);
if ((client_fd1 =
accept(sockfd1, (struct sockaddr *) &remote_addr1,
&sin_size)) == -1) {
perror("accept出错");
continue;
}
if ((client_fd2 =
accept(sockfd2, (struct sockaddr *) &remote_addr2,
&sin_size)) == -1) {
perror("accept出错");
continue;
}
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2, NULL, (void *) send1_function, NULL);
sleep(1);
pthread_create(&thread1, NULL, (void *) send_function, NULL);
sleep(1);
}
/*
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2, NULL, (void *) send1_function, NULL);
sleep(1);
pthread_create(&thread1, NULL, (void *) send_function, NULL);
sleep(1);
*/
}
void send_function()
{
if (send(client_fd1, "Hello ,you are connected!\n", 26, 0) == -1)
perror("send 出错!");
else {
close(client_fd1);
//exit(0);
}
}
void send1_function()
{
if (send(client_fd2, "Hello ,you are connected!\n", 26, 0) == -1)
perror("send 出错!");
else {
close(client_fd2);
//exit(0);
}
}
#1
顶顶
#2
贴代码
#3
高手帮忙啊
#4
//这是服务器端的程序
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#define SERVPORT 3334
#define BACKLOG 10
#define MAXDATASIZE 100
#define SERVPORTT 3333
#define HOSTADDS "127.0.0.1"
int client_fd1;
int client_fd2;
void send_function();
void send1_function();
void recv_function();
main() {
pthread_t thread1,thread2;
int one = 1;
int sockfd1,sockfd2;
int sin_size;
struct sockaddr_in my_addr1,my_addr2;
struct sockaddr_in remote_addr1,remote_addr2;
sockfd1 = socket(AF_INET,SOCK_STREAM,0);
sockfd2 = socket(AF_INET,SOCK_STREAM,0);
if(sockfd1 == -1 || sockfd2 == -1) {
perror("socket创建出错!");
exit(1);
}
memset(&my_addr1,0,sizeof(my_addr1));
my_addr1.sin_family=AF_INET;
my_addr1.sin_port=htons(SERVPORT);
my_addr1.sin_addr.s_addr = inet_addr(HOSTADDS);
memset(&my_addr2,0,sizeof(my_addr2));
my_addr2.sin_family=AF_INET;
my_addr2.sin_port=htons(SERVPORTT);
my_addr2.sin_addr.s_addr = inet_addr(HOSTADDS);
//为了重复绑定IP地址,必须对套接字进行设置
if(setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
if(setsockopt(sockfd2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
//绑定
if(bind(sockfd1,(struct sockaddr *)&my_addr1,sizeof(my_addr1)))
{
printf("发送套接字绑定失败");
exit(1);
}
if(bind(sockfd2,(struct sockaddr *)&my_addr2,sizeof(my_addr2)))
{
printf("接收套接字绑定失败");
exit(1);
}
//侦听
if(listen(sockfd1,BACKLOG) == -1 || listen(sockfd2,BACKLOG)== -1) {
perror("listen出错!");
exit(1);
}
while(1) {
sin_size = sizeof (struct sockaddr_in);
if((client_fd1 = accept(sockfd1,(struct sockaddr *)&remote_addr1,&sin_size)) == -1 ) {
perror("accept出错");
continue;
}
if((client_fd2 = accept(sockfd2,(struct sockaddr *)&remote_addr2,&sin_size)) == -1 ) {
perror("accept出错");
continue;
}
}
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2,NULL,(void *)send1_function,NULL);
sleep(10);
pthread_create(&thread1,NULL,(void *)send_function,NULL);
sleep(10);
}
void send_function() {
if(send (client_fd1,"Hello ,you are connected!\n",26,0) == -1)
perror("send 出错!");
else{
close(client_fd1);
exit(0);
}
}
void send1_function() {
if(send (client_fd2,"Hello ,you are connected!\n",26,0) == -1)
perror("send 出错!");
else{
close(client_fd2);
exit(0);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#define SERVPORT 3334
#define BACKLOG 10
#define MAXDATASIZE 100
#define SERVPORTT 3333
#define HOSTADDS "127.0.0.1"
int client_fd1;
int client_fd2;
void send_function();
void send1_function();
void recv_function();
main() {
pthread_t thread1,thread2;
int one = 1;
int sockfd1,sockfd2;
int sin_size;
struct sockaddr_in my_addr1,my_addr2;
struct sockaddr_in remote_addr1,remote_addr2;
sockfd1 = socket(AF_INET,SOCK_STREAM,0);
sockfd2 = socket(AF_INET,SOCK_STREAM,0);
if(sockfd1 == -1 || sockfd2 == -1) {
perror("socket创建出错!");
exit(1);
}
memset(&my_addr1,0,sizeof(my_addr1));
my_addr1.sin_family=AF_INET;
my_addr1.sin_port=htons(SERVPORT);
my_addr1.sin_addr.s_addr = inet_addr(HOSTADDS);
memset(&my_addr2,0,sizeof(my_addr2));
my_addr2.sin_family=AF_INET;
my_addr2.sin_port=htons(SERVPORTT);
my_addr2.sin_addr.s_addr = inet_addr(HOSTADDS);
//为了重复绑定IP地址,必须对套接字进行设置
if(setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
if(setsockopt(sockfd2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
//绑定
if(bind(sockfd1,(struct sockaddr *)&my_addr1,sizeof(my_addr1)))
{
printf("发送套接字绑定失败");
exit(1);
}
if(bind(sockfd2,(struct sockaddr *)&my_addr2,sizeof(my_addr2)))
{
printf("接收套接字绑定失败");
exit(1);
}
//侦听
if(listen(sockfd1,BACKLOG) == -1 || listen(sockfd2,BACKLOG)== -1) {
perror("listen出错!");
exit(1);
}
while(1) {
sin_size = sizeof (struct sockaddr_in);
if((client_fd1 = accept(sockfd1,(struct sockaddr *)&remote_addr1,&sin_size)) == -1 ) {
perror("accept出错");
continue;
}
if((client_fd2 = accept(sockfd2,(struct sockaddr *)&remote_addr2,&sin_size)) == -1 ) {
perror("accept出错");
continue;
}
}
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2,NULL,(void *)send1_function,NULL);
sleep(10);
pthread_create(&thread1,NULL,(void *)send_function,NULL);
sleep(10);
}
void send_function() {
if(send (client_fd1,"Hello ,you are connected!\n",26,0) == -1)
perror("send 出错!");
else{
close(client_fd1);
exit(0);
}
}
void send1_function() {
if(send (client_fd2,"Hello ,you are connected!\n",26,0) == -1)
perror("send 出错!");
else{
close(client_fd2);
exit(0);
}
}
#5
//这是客户端的
#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 <pthread.h>
#define SERVPORTT 3333
#define SERVPORT 3334
#define MAXDATASIZE 100
#define SERVER_HOST_ADDR "127.0.0.1"
void recv_function();//从3334端口接受数据
void recv1_function();//从3333端口接受数据
main(int argc, char **argv) {
if (argc != 2)
{
printf("Usage: ./%s ServerIPAddress\n",argv[0]);
exit(1);
}
pthread_t thread1,thread2;
pthread_create(&thread1,NULL,(void *)recv1_function,NULL);
sleep(10);
pthread_create(&thread2,NULL,(void *)recv_function,NULL);
sleep(10);
}
void recv_function() {
int sockfd,recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port = htons(SERVPORT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv出错了!");
exit(1);
}
buf[recvbytes]='\0';
printf("Received : %s,%d",buf,SERVPORT);
}
void recv1_function() {
int sockfd,recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port = htons(SERVPORTT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv出错了!");
exit(1);
}
buf[recvbytes]='\0';
printf("Received : %s,%d",buf,SERVPORTT);
}
#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 <pthread.h>
#define SERVPORTT 3333
#define SERVPORT 3334
#define MAXDATASIZE 100
#define SERVER_HOST_ADDR "127.0.0.1"
void recv_function();//从3334端口接受数据
void recv1_function();//从3333端口接受数据
main(int argc, char **argv) {
if (argc != 2)
{
printf("Usage: ./%s ServerIPAddress\n",argv[0]);
exit(1);
}
pthread_t thread1,thread2;
pthread_create(&thread1,NULL,(void *)recv1_function,NULL);
sleep(10);
pthread_create(&thread2,NULL,(void *)recv_function,NULL);
sleep(10);
}
void recv_function() {
int sockfd,recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port = htons(SERVPORT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv出错了!");
exit(1);
}
buf[recvbytes]='\0';
printf("Received : %s,%d",buf,SERVPORT);
}
void recv1_function() {
int sockfd,recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port = htons(SERVPORTT);
serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) == -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1){
perror("recv出错了!");
exit(1);
}
buf[recvbytes]='\0';
printf("Received : %s,%d",buf,SERVPORTT);
}
#6
把你的程序重新整理了一下,有点反应了:
//这是客户端的
#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 <pthread.h>
#define SERVPORTT 3333
#define SERVPORT 3334
#define MAXDATASIZE 100
//#define SERVER_HOST_ADDR "127.0.0.1"
//void recv_function(); //从3334端口接受数据
//void recv1_function(); //从3333端口接受数据
void recv_function(char *server_addr); //从3334端口接受数据
void recv1_function(char *server_addr); //从3333端口接受数据
main(int argc, char **argv)
{
if (argc != 2) {
printf("Usage: ./%s ServerIPAddress\n", argv[0]);
exit(1);
}
pthread_t thread1, thread2;
// pthread_create(&thread1, NULL, (void *) recv1_function, NULL);
// sleep(10);
// pthread_create(&thread2, NULL, (void *) recv_function, NULL);
// sleep(10);
pthread_create(&thread1, NULL, (void *) recv1_function, argv[1]);
sleep(5);
pthread_create(&thread2, NULL, (void *) recv_function, argv[1]);
sleep(5);
}
//void recv_function()
void recv_function(char *server_addr)
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
// serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
serv_addr.sin_addr.s_addr = inet_addr(server_addr);
bzero(&(serv_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
== -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv出错了!");
exit(1);
}
buf[recvbytes] = '\0';
// printf("Received : %s,%d", buf, SERVPORT);
printf("Received : %s,%d\n", buf, SERVPORT);
}
//void recv1_function()
void recv1_function(char *server_addr)
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORTT);
// serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
serv_addr.sin_addr.s_addr = inet_addr(server_addr);
bzero(&(serv_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
== -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv出错了!");
exit(1);
}
buf[recvbytes] = '\0';
// printf("Received : %s,%d", buf, SERVPORTT);
printf("Received : %s,%d\n", buf, SERVPORTT);
}
//这是客户端的
#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 <pthread.h>
#define SERVPORTT 3333
#define SERVPORT 3334
#define MAXDATASIZE 100
//#define SERVER_HOST_ADDR "127.0.0.1"
//void recv_function(); //从3334端口接受数据
//void recv1_function(); //从3333端口接受数据
void recv_function(char *server_addr); //从3334端口接受数据
void recv1_function(char *server_addr); //从3333端口接受数据
main(int argc, char **argv)
{
if (argc != 2) {
printf("Usage: ./%s ServerIPAddress\n", argv[0]);
exit(1);
}
pthread_t thread1, thread2;
// pthread_create(&thread1, NULL, (void *) recv1_function, NULL);
// sleep(10);
// pthread_create(&thread2, NULL, (void *) recv_function, NULL);
// sleep(10);
pthread_create(&thread1, NULL, (void *) recv1_function, argv[1]);
sleep(5);
pthread_create(&thread2, NULL, (void *) recv_function, argv[1]);
sleep(5);
}
//void recv_function()
void recv_function(char *server_addr)
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
// serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
serv_addr.sin_addr.s_addr = inet_addr(server_addr);
bzero(&(serv_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
== -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv出错了!");
exit(1);
}
buf[recvbytes] = '\0';
// printf("Received : %s,%d", buf, SERVPORT);
printf("Received : %s,%d\n", buf, SERVPORT);
}
//void recv1_function()
void recv1_function(char *server_addr)
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct sockaddr_in serv_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socker创建出错!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORTT);
// serv_addr.sin_addr.s_addr = htons(INADDR_ANY);;
serv_addr.sin_addr.s_addr = inet_addr(server_addr);
bzero(&(serv_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
== -1) {
perror("connect出错了!");
exit(1);
}
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv出错了!");
exit(1);
}
buf[recvbytes] = '\0';
// printf("Received : %s,%d", buf, SERVPORTT);
printf("Received : %s,%d\n", buf, SERVPORTT);
}
#7
服务器端:
//这是服务器端的程序
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#define SERVPORT 3334
#define BACKLOG 10
#define MAXDATASIZE 100
#define SERVPORTT 3333
//#define HOSTADDS "127.0.0.1"
int client_fd1;
int client_fd2;
void send_function();
void send1_function();
void recv_function();
main()
{
pthread_t thread1, thread2;
int one = 1;
int sockfd1, sockfd2;
int sin_size;
struct sockaddr_in my_addr1, my_addr2;
struct sockaddr_in remote_addr1, remote_addr2;
sockfd1 = socket(AF_INET, SOCK_STREAM, 0);
sockfd2 = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd1 == -1 || sockfd2 == -1) {
perror("socket创建出错!");
exit(1);
}
memset(&my_addr1, 0, sizeof(my_addr1));
my_addr1.sin_family = AF_INET;
my_addr1.sin_port = htons(SERVPORT);
my_addr1.sin_addr.s_addr = htons(INADDR_ANY);
memset(&my_addr2, 0, sizeof(my_addr2));
my_addr2.sin_family = AF_INET;
my_addr2.sin_port = htons(SERVPORTT);
my_addr2.sin_addr.s_addr = htons(INADDR_ANY);
//为了重复绑定IP地址,必须对套接字进行设置
if (setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) ==
-1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
if (setsockopt(sockfd2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) ==
-1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
//绑定
if (bind(sockfd1, (struct sockaddr *) &my_addr1, sizeof(my_addr1))) {
printf("发送套接字绑定失败");
exit(1);
}
if (bind(sockfd2, (struct sockaddr *) &my_addr2, sizeof(my_addr2))) {
printf("接收套接字绑定失败");
exit(1);
}
//侦听
if (listen(sockfd1, BACKLOG) == -1 || listen(sockfd2, BACKLOG) == -1) {
perror("listen出错!");
exit(1);
}
while (1) {
sin_size = sizeof(struct sockaddr_in);
if ((client_fd1 =
accept(sockfd1, (struct sockaddr *) &remote_addr1,
&sin_size)) == -1) {
perror("accept出错");
continue;
}
if ((client_fd2 =
accept(sockfd2, (struct sockaddr *) &remote_addr2,
&sin_size)) == -1) {
perror("accept出错");
continue;
}
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2, NULL, (void *) send1_function, NULL);
sleep(1);
pthread_create(&thread1, NULL, (void *) send_function, NULL);
sleep(1);
}
/*
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2, NULL, (void *) send1_function, NULL);
sleep(1);
pthread_create(&thread1, NULL, (void *) send_function, NULL);
sleep(1);
*/
}
void send_function()
{
if (send(client_fd1, "Hello ,you are connected!\n", 26, 0) == -1)
perror("send 出错!");
else {
close(client_fd1);
//exit(0);
}
}
void send1_function()
{
if (send(client_fd2, "Hello ,you are connected!\n", 26, 0) == -1)
perror("send 出错!");
else {
close(client_fd2);
//exit(0);
}
}
//这是服务器端的程序
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#define SERVPORT 3334
#define BACKLOG 10
#define MAXDATASIZE 100
#define SERVPORTT 3333
//#define HOSTADDS "127.0.0.1"
int client_fd1;
int client_fd2;
void send_function();
void send1_function();
void recv_function();
main()
{
pthread_t thread1, thread2;
int one = 1;
int sockfd1, sockfd2;
int sin_size;
struct sockaddr_in my_addr1, my_addr2;
struct sockaddr_in remote_addr1, remote_addr2;
sockfd1 = socket(AF_INET, SOCK_STREAM, 0);
sockfd2 = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd1 == -1 || sockfd2 == -1) {
perror("socket创建出错!");
exit(1);
}
memset(&my_addr1, 0, sizeof(my_addr1));
my_addr1.sin_family = AF_INET;
my_addr1.sin_port = htons(SERVPORT);
my_addr1.sin_addr.s_addr = htons(INADDR_ANY);
memset(&my_addr2, 0, sizeof(my_addr2));
my_addr2.sin_family = AF_INET;
my_addr2.sin_port = htons(SERVPORTT);
my_addr2.sin_addr.s_addr = htons(INADDR_ANY);
//为了重复绑定IP地址,必须对套接字进行设置
if (setsockopt(sockfd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) ==
-1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
if (setsockopt(sockfd2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) ==
-1) {
printf("setsockopt SO_REUSEADDR error\n");
exit(1);
}
//绑定
if (bind(sockfd1, (struct sockaddr *) &my_addr1, sizeof(my_addr1))) {
printf("发送套接字绑定失败");
exit(1);
}
if (bind(sockfd2, (struct sockaddr *) &my_addr2, sizeof(my_addr2))) {
printf("接收套接字绑定失败");
exit(1);
}
//侦听
if (listen(sockfd1, BACKLOG) == -1 || listen(sockfd2, BACKLOG) == -1) {
perror("listen出错!");
exit(1);
}
while (1) {
sin_size = sizeof(struct sockaddr_in);
if ((client_fd1 =
accept(sockfd1, (struct sockaddr *) &remote_addr1,
&sin_size)) == -1) {
perror("accept出错");
continue;
}
if ((client_fd2 =
accept(sockfd2, (struct sockaddr *) &remote_addr2,
&sin_size)) == -1) {
perror("accept出错");
continue;
}
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2, NULL, (void *) send1_function, NULL);
sleep(1);
pthread_create(&thread1, NULL, (void *) send_function, NULL);
sleep(1);
}
/*
//建立两个线程,通过不同的端口同时向客户端发送数据
pthread_create(&thread2, NULL, (void *) send1_function, NULL);
sleep(1);
pthread_create(&thread1, NULL, (void *) send_function, NULL);
sleep(1);
*/
}
void send_function()
{
if (send(client_fd1, "Hello ,you are connected!\n", 26, 0) == -1)
perror("send 出错!");
else {
close(client_fd1);
//exit(0);
}
}
void send1_function()
{
if (send(client_fd2, "Hello ,you are connected!\n", 26, 0) == -1)
perror("send 出错!");
else {
close(client_fd2);
//exit(0);
}
}