不是綁定了學校的網路IP,你的數據包會走伺服器的網關192.168.1.1。像這種學校的網管設備一般會帶有防火牆之類的過濾功能。 如果伺服器開啟了這樣類似的功能,那你的報文可能就被過濾掉了。 你可以用抓包軟體抓一下包看看報文到底發出去沒,或者收到沒。
Ⅱ Linux下C語言socket編程
socket編程一般是基於tcp或者udp協議來寫的,你的問題很抽象,我不知道你要的是基於tcp還是udp的socket編譯。我把相對難的基於tcp協議的socket編譯給你吧。你想看懂我的代碼需要知道tcp的三次握手機制。否則我寫了注釋,你看代碼也有點困難。 ////服務端的代碼
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/socket.h>
4 #include <unistd.h>
5 #include <netinet/in.h>
6 #include <sys/stat.h>
7 #include <stdlib.h>
8 #include <arpa/inet.h>
9
10 #define LOCAL_PORT 1234
11 #define MAX_LEN 512
12 #define MAX_NUM 5
13
14 int main(int argc, char *argv[])
15 {
16 int sock_fd, sock_data;
17 int ret, len_addr;
18 char buf[MAX_LEN];
19 ssize_t len;
20 struct sockaddr_in local_addr, remote_addr;
21
22 sock_fd = socket(AF_INET, SOCK_STREAM, 0); //創建套接字,sock_fd是套接字描述符,類似我們的身份證號碼
23 if (sock_fd < 0)
24 {
25 perror("socket()");
26 return sock_fd;
27 }
28
29 local_addr.sin_family = AF_INET;// 協議族,ipv4
30 local_addr.sin_port = htons(LOCAL_PORT);// 把伺服器埠轉換成網路位元組序
31 local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//把字元串形式的ip轉換成網路位元組序
32
33 ret = bind(sock_fd, (struct sockaddr *)&local_addr, (size_t)sizeof(local_addr));// 把sock_fd和本機ip,埠邦定
34 if (ret < 0)
35 {
36 perror("bind()");
37 close(sock_fd);
38 return ret;
39 }
40
41 ret = listen(sock_fd, MAX_NUM);//監聽socket
42 if (ret)
43 {
44 perror("listen()");
45 close(sock_fd);
46 return ret;
47 }
48
49 memset(buf, 0, MAX_LEN);
50
51 len_addr = sizeof(remote_addr);
52
53 sock_data = accept(sock_fd, (struct sockaddr *)&remote_addr, (socklen_t *)&len_addr);//接受客戶端的連接
54 if (ret < 0)
55 {
56 perror("accept()");
57 close(sock_fd);
58 return ret;
59 }
60
61 while (1)
62 {
63 int slen;
64 len = recv(sock_data, buf, MAX_LEN, 0);//接受客戶端的數據
65 if (len < 0)
66 {
67 perror("recv()");
68 close(sock_data);
69 close(sock_fd);
70 return len;
71 }
72 printf("%s\n", buf);
73
74 slen = send(sock_data, "congratulation!", 15, 0);//向客戶端發送數據
75 if (slen <= 0)
76 {
77 printf("slen = %d\n", slen);
78 perror("send()");
79 close(sock_data);
80 close(sock_fd);
81 return slen;
82 }
83 sleep(1);
84 }
85
86 close(sock_data);
87 close(sock_fd);
88
89 return 0;
90 }
////////////客戶端的代碼 1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/socket.h>
4 #include <unistd.h>
5 #include <netinet/in.h>
6 #include <sys/stat.h>
7 #include <stdlib.h>
8 #include <arpa/inet.h>
9
10 #define REMOTE_PORT 1234
11 #define MAX_LEN 512
12
13 int main(int argc, char *argv[])
14 {
15 int sock_fd, ret;
16 int len;
17 char buf[MAX_LEN];
18 struct sockaddr_in local_addr, remote_addr;
19
20 sock_fd = socket(AF_INET, SOCK_STREAM, 0);
21 if (sock_fd < 0)
22 {
23 perror("socket()");
24 return sock_fd;
25 }
26
28 local_addr.sin_family = AF_INET;
29 local_addr.sin_addr.s_addr = htonl(INADDR_ANY); //自動獲取本機的ip地址
30 local_addr.sin_port = htons(0); //隨機選取可用的埠,並不是指定埠為0
31
33 remote_addr.sin_family= AF_INET;
34 remote_addr.sin_port = htons(REMOTE_PORT);
35 ret = inet_aton("127.0.0.1", &remote_addr.sin_addr);
36
38 ret = bind(sock_fd, (struct sockaddr *)&local_addr, sizeof(local_addr)); //把本機的ip,port和socket綁定
39 if (ret < 0)
40 {
41 perror("bind() !");
42 close(sock_fd);
43 return ret;
44 }
45
47 ret = connect(sock_fd, (struct sockaddr *)&remote_addr, (socklen_t)sizeof(remote_addr)); //把本機的socket和對方的port,ip建立連接
48 if (ret < 0)
49 {
50 perror("connect()");
51 close(sock_fd);
52 return ret;
53 }
54
55 memset(buf, 0, MAX_LEN);
56
57 while (1)
58 {
59 int i;
60 // len = send(sock_fd, buf, (size_t)MAX_LEN, 0);
61 len = send(sock_fd, "hello", 6, 0);
62 if (len <= 0)
63 {
64 perror("send()");
65 close(sock_fd);
66 return ret;
67 }
68
69 // printf("%d-->bytes send!\n", len);
70 sleep(1);
71
72 len = recv(sock_fd, buf, MAX_LEN, 0);
73 if (len <= 0)
74 {
75 perror("recv()");
76 close(sock_fd);
77 return ret;
78 }
79
80 for (i = 0; i < len; i++)
81 {
82 printf("%c", buf[i]);
83 }
84 printf("\n");
85 }
86
87 close(sock_fd);
88
89 return 0;
90 }
你把服務端和客戶端這兩個程序分別保存為server.c和client.c。然後編譯gcc server.c -o server,gcc client .c -o client。運行時先運行服務端,用命令./server,再運行客戶端,用命令./client。 注意運行命令是「點 斜杠」,「點」表示當前目錄。
Ⅲ 求助linux下socket多線程編程的例子
linux socket跟文件描述符一樣,在內核態對應了file類型的數據結構。但是對於用戶態進程而言,其標識符即文件描述符,跟操作文件的文件描述符完全一樣,是整型值。可以像close文件描述符一樣通過close函數來關閉socket 文件描述符。
Ⅳ linux socket編程代碼
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
char *host_name="127.0.0.1";
int port=7778;
struct student
{
char name[20];
char num[20];
float score;
}t={"xiejian","200701415",89.9};
int main ( )
{
char buf[502];
int socket_descriptor;
struct sockaddr_in pin;
bzero(&pin,sizeof(pin));
pin.sin_family=AF_INET;
inet_pton(AF_INET,host_name,&pin.sin_addr);
pin.sin_port=htons(port);
if((socket_descriptor=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Error openung socket!\n");
exit(1);
}
if(connect(socket_descriptor,(void *)&pin,sizeof (pin))==-1)
{
perror("can not connecting to server!\n");
exit (1);
}
printf("Send message to server ...\n");
//memset(buf,0,502);
//memcpy(buf,(void *)&t,sizeof(t));
//sprintf(buf,"%s %d",t.name,t.num);
//printf("the first string : %s\n",buf);
if(send(socket_descriptor,(void *) &t,sizeof(t),0)==-1)
{
perror("can not send message!\n");
exit (1);
}
printf("waiting for response from server!\n");
memset(buf,0,502);
if(recv(socket_descriptor,buf,sizeof(buf),0)==-1)
{
perror("can not receive response !\n");
exit (1);
}
printf("\n Response from server : \n");
memcpy((struct student *)&t,buf,sizeof(buf));
//printf("the string : %s \n",rebuf);
printf("--%s--%s--%5.1f--\n",t.name,t.num,t.score);
close (socket_descriptor);
}
/* 注意在send 結構體時應該把結構體強制類型轉換為void * 型
** 接受之後又要強制轉換回結構體型!否則則穿過來的是結構體的
** 一部分!
*/
下面是服務端
#include<stdio.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netdb.h>
int port=7778;
struct student
{
char name[20];
char num[20];
float score;
}t;
main()
{
struct sockaddr_in sin;
struct sockaddr_in pin;
int sock_descriptor;
int temp_sock_descriptor;
int size_of_addr;
//struct cfg a;
char buf[502];
int i,lenth;
sock_descriptor=socket(AF_INET,SOCK_STREAM,0);
if(sock_descriptor==-1)
{
perror("socket!\n");
exit(1);
}
bzero(&sin,sizeof(sin));
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_port=htons(port);
if(bind(sock_descriptor,(struct sockaddr *)&sin,sizeof (sin))==-1)
{
perror("bind!\n");
exit(1);
}
if(listen (sock_descriptor,20)==-1)
{
perror("listen!\n");
exit(1);
}
printf("Waiting for accepting connection from client!\n");
while(1)
{
printf("the process is waiting here!\n");
temp_sock_descriptor=accept(sock_descriptor, (struct sockaddr *)&pin,&size_of_addr);
if(temp_sock_descriptor==-1)
{
perror("call to accept!\n");
exit (1);
}
memset(buf,0,502);
if(recv(temp_sock_descriptor,buf,sizeof(buf),0)==-1)
{
perror("recv!\n");
exit (1);
}
printf("received : \n");
//printf("the recv buf is :%s\n",buf);
memcpy((struct student *) &t,buf,sizeof(buf));
printf("--%s--%s--%5.1f--\n",t.name,t.num,t.score);
if(send (temp_sock_descriptor,(void *) &t,sizeof(t),0)==-1)
{
perror("send!\n");
exit(1);
}
close (temp_sock_descriptor);
}
}
Ⅳ 求一個linux下使用socket傳輸較多數據的編程實例,不要那種簡單連上發個hello就結束的
socket傳輸較多數據,socket 每次最多傳輸1K。數據量大就在recv時寫個while循環就行了,每次判斷是否接受完畢。
Ⅵ linux下socket編程,菜鳥求解。。。
linux-u4p5:~/test/socket/server # ./client
connect ok,waiting for the server's message back
程序好像沒看出問題來啊
會不會是防火牆設置問題?
Ⅶ linux socket編程相關
你再調用一下 getsockopt 看看 SO_REUSEADDR 設置成功了沒有。
Ⅷ 求linux下的socket 編程
看這個代碼你最好了解一下tcp協議,了解一下三次握手機制,要不然你可能看不懂,當初我們學到這的時候學了一天的tcp....還有,就是把下面代碼分開放到2個.c文件中,必須先運行伺服器端,再運行客戶端
//伺服器端的代碼
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/socket.h>
4 #include <unistd.h>
5 #include <netinet/in.h>
6 #include <sys/stat.h>
7 #include <stdlib.h>
8 #include <arpa/inet.h>
9
10 #define LOCAL_PORT 1234
11 #define MAX_LEN 512
12 #define MAX_NUM 5
13
14 int main(int argc, char *argv[])
15 {
16 int sock_fd, sock_data;
17 int ret, len_addr;
18 char buf[MAX_LEN];
19 ssize_t len;
20 struct sockaddr_in local_addr, remote_addr;
21
22 sock_fd = socket(AF_INET, SOCK_STREAM, 0); //創建套接字,sock_fd是套接字描述符,類似我們的身份證號碼
23 if (sock_fd < 0)
24 {
25 perror("socket()");
26 return sock_fd;
27 }
28
29 local_addr.sin_family = AF_INET;// 協議族,ipv4
30 local_addr.sin_port = htons(LOCAL_PORT);// 把伺服器埠轉換成網路位元組序
31 local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//把字元串形式的ip轉換成網路位元組序
32
33 ret = bind(sock_fd, (struct sockaddr *)&local_addr, (size_t)sizeof(local_addr));// 把sock_fd和本機ip,埠邦定
34 if (ret < 0)
35 {
36 perror("bind()");
37 close(sock_fd);
38 return ret;
39 }
40
41 ret = listen(sock_fd, MAX_NUM);//監聽socket
42 if (ret)
43 {
44 perror("listen()");
45 close(sock_fd);
46 return ret;
47 }
48
49 memset(buf, 0, MAX_LEN);
50
51 len_addr = sizeof(remote_addr);
52
53 sock_data = accept(sock_fd, (struct sockaddr *)&remote_addr, (socklen_t *)&len_addr);//接受客戶端的連接
54 if (ret < 0)
55 {
56 perror("accept()");
57 close(sock_fd);
58 return ret;
59 }
60
61 while (1)
62 {
63 int slen;
64 len = recv(sock_data, buf, MAX_LEN, 0);//接受客戶端的數據
65 if (len < 0)
66 {
67 perror("recv()");
68 close(sock_data);
69 close(sock_fd);
70 return len;
71 }
72 printf("%s\n", buf);
73
74 slen = send(sock_data, "congratulation!", 15, 0);//向客戶端發送數據
75 if (slen <= 0)
76 {
77 printf("slen = %d\n", slen);
78 perror("send()");
79 close(sock_data);
80 close(sock_fd);
81 return slen;
82 }
83 sleep(1);
84 }
85
86 close(sock_data);
87 close(sock_fd);
88
89 return 0; }
//客戶端代碼
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/socket.h>
4 #include <unistd.h>
5 #include <netinet/in.h>
6 #include <sys/stat.h>
7 #include <stdlib.h>
8 #include <arpa/inet.h>
9
10 #define REMOTE_PORT 1234
11 #define MAX_LEN 512
12
13 int main(int argc, char *argv[])
14 {
15 int sock_fd, ret;
16 int len;
17 char buf[MAX_LEN];
18 struct sockaddr_in local_addr, remote_addr;
19
20 sock_fd = socket(AF_INET, SOCK_STREAM, 0);
21 if (sock_fd < 0)
22 {
23 perror("socket()");
24 return sock_fd;
25 }
26
28 local_addr.sin_family = AF_INET;
29 local_addr.sin_addr.s_addr = htonl(INADDR_ANY); //自動獲取本機的ip地址
30 local_addr.sin_port = htons(0); //隨機選取可用的埠,並不是指定埠為0
31
33 remote_addr.sin_family= AF_INET;
34 remote_addr.sin_port = htons(REMOTE_PORT);
35 ret = inet_aton("127.0.0.1", &remote_addr.sin_addr);
36
38 ret = bind(sock_fd, (struct sockaddr *)&local_addr, sizeof(local_addr)); //把本機的ip,port和socket綁定
39 if (ret < 0)
40 {
41 perror("bind() !");
42 close(sock_fd);
43 return ret;
44 }
45
47 ret = connect(sock_fd, (struct sockaddr *)&remote_addr, (socklen_t)sizeof(remote_addr)); //把本機的socket和對方的port,ip建立連接
48 if (ret < 0)
49 {
50 perror("connect()");
51 close(sock_fd);
52 return ret;
53 }
54
55 memset(buf, 0, MAX_LEN);
56
57 while (1)
58 {
59 int i;
60 // len = send(sock_fd, buf, (size_t)MAX_LEN, 0);
61 len = send(sock_fd, "hello", 6, 0);
62 if (len <= 0)
63 {
64 perror("send()");
65 close(sock_fd);
66 return ret;
67 }
68
69 // printf("%d-->bytes send!\n", len);
70 sleep(1);
71
72 len = recv(sock_fd, buf, MAX_LEN, 0);
73 if (len <= 0)
74 {
75 perror("recv()");
76 close(sock_fd);
77 return ret;
78 }
79
80 for (i = 0; i < len; i++)
81 {
82 printf("%c", buf[i]);
83 }
84 printf("\n");
85 }
86
87 close(sock_fd);
88
89 return 0;
90 }