当前位置:首页 » 操作系统 » linuxsocket程序

linuxsocket程序

发布时间: 2022-08-02 09:44:07

1. linux socket是什么意思

基于Linux的SOCKET编程

2. linux中socket是如何调用驱动程序

分为发送和接受:
发送:
首先,socketAPI会创建并把数据至一个叫sk_buff的结构体,然后依次把sk_buff交给运输层,网络层,数据链路层协议进行处理,然后在填写完sk_buff后再把他交付给驱动程序由网络设备发送出去。
接受和发送是反过程,驱动层程序一般由中断处理收到数据包后会创建sk_buff结构体,让后把数据和一些控制信息填进去,再把sk_buff向数据链路层协议交付,然后就是网络层,运输层最后交给socketAPI接口了

3. linux下的socket编程

同学,你希望从哪个方面入手进行完善呢?或者说你遇到什么问题?!!

你的程序健壮性是很不错的,就是太复杂,我给个最简单的。接收和显示放一块:
void read_and_echo(in fd)
{
char buf[512]={0};
int rc;
while (1)
{
rc=read(fd,buf,511);// 读入511个字符
buf[rc]=0;// 截断
printf("Reveive Message: %s",buf);
}
}

4. 在Linux系统下编写一个socket程序

我给你一个更高端的
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <string.h>
#define M 8888

struct qun
{
int cy[5];
};

struct haoyou
{
int py[5];
};

struct ri
{
int geren[5];
};

int main()
{
struct qun group[5]={0};
struct haoyou pyd[5]={0};
struct ri ziji[5]={0};

printf("server onil\n");
printf("this port is %d\n",M);

fd_set rest;
int servsock=socket(AF_INET,SOCK_STREAM,0);

if(-1==servsock)
{
perror("socket perror");
return 1;
}

struct sockaddr_in servaddr;
struct sockaddr_in clienaddr;

servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(M);

int a=bind(servsock,(struct sockaddr *)&servaddr,sizeof(servaddr));
if(-1==a)

{
perror("bind perror\n");
return 1;
}

int b=listen(servsock,5);
if(-1==b)

{
perror("listen perror");
return 1;
}

FD_ZERO(&rest);
char sendbuff[1024];
char recvbuff[1024];
int n;
int qw;
int r[10];
int min[1024]={0};
int i=0;
int iWho[10]={0};
int mabi[1024]={0};
char mingzi[10][50]={0};
char caonima[1024]={0};
int wocao[10]={0};
n=sizeof(clienaddr);
struct timeval time;
while(1)
{
time.tv_sec = 0;
time.tv_usec = 470;
FD_SET(servsock,&rest);
FD_SET(fileno(stdin),&rest);
select(servsock+1,&rest,NULL,NULL,&time);
if(FD_ISSET(servsock,&rest))
{
r[i]=accept(servsock,(struct sockaddr *)&clienaddr,&n);
if(r[i]==-1)
{
i--;
}
i++;
}
int p;
for(p=0;p<i;p++)
{
FD_SET(r[p],&rest);
}
int ret=select(r[i-1]+1,&rest,NULL,NULL,&time);
if(ret<0)
{
printf("socket error\n");
continue;
}
else
{
int u;
for(u=0;u<i;u++

5. linux编程socket程序的编译语句

server端:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include<string.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define MAX_LINE 100

int main()
{
struct sockaddr_in server;
struct sockaddr_in client;

int server_fd;
int client_fd;

socklen_t len;

char buf[MAX_LINE]; //用于存储传送内容到缓冲区
char addr_ip[INET_ADDRSTRLEN];//存储客户端地址缓冲区
int port = 8000;//端口号8000
int n; //读写字节数

bzero(&server,sizeof(server));//清空地址结构
server.sin_family = AF_INET;//使用ipv4
server.sin_addr.s_addr = INADDR_ANY;//服务器可以接受任意地址
server.sin_port = htons(port);//把端口号转成网络字节序

server_fd = socket(AF_INET,SOCK_STREAM,0);//创立套接字,使用tcp协议
bind(server_fd,(struct sockaddr *)&server,sizeof(server));//将地址和套接字绑定
listen(server_fd,10);//开始监听

printf("waiting```\n");

while(1)
{
client_fd = accept(server_fd,(struct sockaddr *)&client,&len);
n = read(client_fd,buf,MAX_LINE);//读取客户端来的消息
inet_ntop(AF_INET,&client.sin_addr,addr_ip,sizeof(addr_ip));

printf("client ip is : %s , port is : %d\n",addr_ip,ntohs(client.sin_port));
printf("content is : %s\n",buf);//打印客户端信息

write(client_fd,buf,n);//转发回去给客户端
close(client_fd);
}

if(close(client_fd) == -1)
{
perror("fail to close");
exit(1);
}

return 0;
}

client端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define MAX_LINE 100

int main(int argc,char * argv[])
{
struct sockaddr_in server;//服务器地址
char buf[MAX_LINE];

int server_fd;
int port = 8000;

char *str = "test string";//默认字符串

if(argc > 1)
{
str = argv[1];//从命令参数中取得用户输入的串
}

bzero(&server,sizeof(server));//清空地址结构

server.sin_family = AF_INET;//使用ipv4

inet_pton(AF_INET,"192.168.228.129",&server.sin_addr);
server.sin_port = htons(port);

server_fd = socket(AF_INET,SOCK_STREAM,0);//创建套接字

connect(server_fd,(struct sockaddr *)&server,sizeof(server));

write(server_fd,str,strlen(str) + 1);
//因为是本机测试,所以不用考虑延时
read(server_fd,buf,MAX_LINE);

printf("recieve from server :%s\n",buf);
close(server_fd);

return 0;
}

//源码来自于网络,谁写也都类似,都是基于tcp的三次握手协议来写的

6. linux socket程序 求代码运行结果

ret=write(socket_fd,send_buff,strlen(send_buff));

应该改为:

ret=write(new_socket_fd,send_buff,strlen(send_buff));

accept成功后,所有的操作,都要针对新获得的句柄来操作,不然会引发错误,造成主程序异常退出。

7. 在linux下如何用c语言来写一个socket编程的聊天小程序

源码如下:
//chat_one.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>

int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chatone_fd;
int ret,i;
int len, msg_len;
fd_set fdset;

if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}

printf("server address is %s\n", argv[1]);

chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
inet_pton(AF_INET, argv[1], &chattwo.sin_addr.s_addr);

chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
chatone.sin_addr.s_addr = INADDR_ANY;

chatone_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chatone_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}

ret = bind(chatone_fd, (struct sockaddr *)&chatone, sizeof(chatone));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}

for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chatone_fd, &fdset);

if ( -1 == select ( chatone_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}

if ( FD_ISSET( chatone_fd, &fdset)){
recvfrom( chatone_fd, pmsg, 999, 0, NULL, 0);
printf("receive %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chattwo);
sendto( chatone_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chattwo, len);

printf("send %s\n", pmsg);
}
}

printf("sent %d packets\n", i);

close(chatone_fd);
return 0;
}
//chat_two.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>

int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chattwo_fd;
int ret,i;
int len, msg_len;
fd_set fdset;

if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}

printf("server address is %s\n", argv[1]);

chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
chattwo.sin_addr.s_addr = INADDR_ANY;

chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
inet_pton(AF_INET, argv[1], &chatone.sin_addr.s_addr);

chattwo_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chattwo_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}

ret = bind(chattwo_fd, (struct sockaddr *)&chattwo, sizeof(chattwo));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}

for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chattwo_fd, &fdset);

if ( -1 == select ( chattwo_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}

if ( FD_ISSET( chattwo_fd, &fdset)){
recvfrom( chattwo_fd, pmsg, 999, 0, NULL, 0);
printf("receive: %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chatone);
sendto( chattwo_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chatone, len);

printf("send %s\n", pmsg);
}
}

printf("sent %d packets\n", i);

close(chattwo_fd);
return 0;
}
编译好这个两个程序就可以进行简单的通信了。

8. linux socket 的程序如何在windows下编译并运行

本质区别是这样的,linux下用的是伯克利socket,windows底下用的WinSocket.
两者其实是大同小异的,不同的地方在以下几点:
1 头文件不同,在linux下用到的关键头文件<sys/socket.h>
windows下用到的是 <Winsock2.h>

2 socket的初始化不一样,在windows下要有一个WSAStartup,而linux 下没有。

3 具体的细节不一样,linux下的c和windows下的c的一些语法有些出入,你自己找找。

下面我把windows下的socket通讯的最基本的结构给你,你对照着修改就可以了,一下的程序我调试了一下,编译过了。
客户端
#include <Winsock2.h>
#include <stdio.h>

void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);

SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));

char recvBuf[100];
recv(sockClient,recvBuf,100,0);
printf("%s\n",recvBuf);
send(sockClient,"This is lisi",strlen("This is lisi")+1,0);

closesocket(sockClient);
WSACleanup();
}

服务器端
#include <Winsock2.h>
#include <stdio.h>

void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);

SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);

bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));

listen(sockSrv,5);

SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);

while(1)
{
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[100];
sprintf(sendBuf,"来自服务器端",
inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[100];
recv(sockConn,recvBuf,100,0);
printf("%s\n",recvBuf);
closesocket(sockConn);
}
}

9. 求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 }

10. 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);
}
}

热点内容
垂直式16m缓存 发布:2025-01-18 05:25:48 浏览:926
梦幻诛仙2护送脚本 发布:2025-01-18 05:25:45 浏览:321
人间地狱为什么没有服务器 发布:2025-01-18 05:20:07 浏览:442
c语言编译器的版权 发布:2025-01-18 05:13:37 浏览:296
htmlbase64图片上传 发布:2025-01-18 05:13:03 浏览:19
微信小程序源码目录 发布:2025-01-18 05:08:51 浏览:679
投影仪什么配置好 发布:2025-01-18 05:01:46 浏览:56
倾城密码有什么蛋糕 发布:2025-01-18 05:00:35 浏览:413
安卓应用锁是什么意思 发布:2025-01-18 04:59:57 浏览:908
mfc数据存储 发布:2025-01-18 04:59:05 浏览:570