ubuntu下用gcc调试UDP socket~~

        GCC真的很强大,这几天调试UDP socket的程序总下意识的以为GCC调试的时候不能接受标准输入,这让我很为难,刚才正巧外面闪电,我于是先用一个小程序试一试,果然推翻我原来浅薄想法:

#include<stdio.h>

int main()
{
	int i;
	scanf("%d",&i);
	printf("get number is: %d\n",i);
	return 0;
}

在ubuntu 11.10 下打开终端,用GDB调试:

(gdb) file try.debug
Reading symbols from /home/kl/code/2/try.debug...done.
(gdb) l
1	#include<stdio.h>
2	
3	int main()
4	{
5		int i;
6		scanf("%d",&i);
7		printf("get number is: %d\n",i);
8		return 0;
9	}
(gdb) break 6
Breakpoint 1 at 0x804843d: file try.c, line 6.
(gdb) break 7
Breakpoint 2 at 0x8048452: file try.c, line 7.
(gdb) run
Starting program: /home/kl/code/2/try.debug 

Breakpoint 1, main () at try.c:6
6		scanf("%d",&i);
(gdb) print i
$1 = 2797556
(gdb) next
1234

Breakpoint 2, main () at try.c:7
7		printf("get number is: %d\n",i);
(gdb) print i
$2 = 1234
(gdb)

在(gdb)模式下,输入next在跳到下一个中断之前会给输入的机会,用print函数证实如上,真的很开心,但翻过头来想,自己又是这么初级。。。于是,我又以这种想法再次调试我的UDP socket程序:

(gdb) file udps.debug
Reading symbols from /home/kl/code/2/udps.debug...done.
(gdb) l
warning: Source file is more recent than executable.
2	#include<netinet/in.h>
3	#include<arpa/inet.h>
4	#include<unistd.h>
5	#include<sys/types.h>
6	#include<sys/socket.h>
7	
8	#define lport 4567
9	#define lip "127.0.0.1"
10	int main()
11	{
(gdb) 
12		int mysock,len;
13		struct sockaddr_in addr;
14		int i=0;
15		char msg[256];
16	
17		int addr_len;
18	
19		if((mysock=socket(AF_INET,SOCK_DGRAM,0))<0)
20		{
21			perror("error");
(gdb) 
22			exit(1);
23		}
24		else
25		{
26			printf("socket created.\n");
27			printf("socket ID: %d\n",mysock);
28		}
29		addr_len=sizeof(struct sockaddr_in);
30		bzero(&addr,sizeof(addr));
31		addr.sin_family=AF_INET;
(gdb) 
32		addr.sin_port=htons(lport);
33		addr.sin_addr.s_addr=inet_addr(lip);
34	
35		if(bind(mysock,&addr,sizeof(addr))<0)
36		{
37			perror("connect");
38			exit(1);
39		}
40		else
41		{
(gdb) 
42			printf("bind ok.\n");
43			printf("local port: %d\n",lport);
44		}
45		while(1)
46		{
47			bzero(msg,sizeof(msg));
48			len=recvfrom (mysock,msg,sizeof(msg),0,&addr,&addr_len);
49	
50			printf("%d: ",i);
51			i++;
(gdb) 
52			printf("message form: %s\n",inet_ntoa(addr.sin_addr));
53			printf("message: %s\n\n",msg);
54			sendto(mysock,msg,len,0,&addr,addr_len);
55		}
56		return 0;
57	}
(gdb) break 48
Breakpoint 1 at 0x8048716: file udps.c, line 48.
(gdb) break 50
Breakpoint 2 at 0x8048751: file udps.c, line 50.
(gdb) break 52
Breakpoint 3 at 0x804876b: file udps.c, line 52.
(gdb) break 53
Breakpoint 4 at 0x804878b: file udps.c, line 53.
(gdb) run
Starting program: /home/kl/code/2/udps.debug 
socket created.
socket ID: 7
bind ok.
local port: 4567

Breakpoint 1, main () at udps.c:48
48			len=recvfrom (mysock,msg,sizeof(msg),0,&addr,&addr_len);
(gdb) print mysock
$1 = 7
(gdb) print msg
$2 = '\000' <repeats 255 times>
(gdb) next

最后一行输入next后就是运行recvfrom函数了,于是我打开终端运行client程序:

kl@kl-Latitude-D630:~/code/2$ ./udpc
socket created.
socket id: 3
romote ip: 127.0.0.1
remote port: 4567
hello

Input message: hello

client程序运行顺利,输入"hello"成功,但是在server的调试终端始终没有显示,这和我直接运行server和client程序的结果是一样的,没有达到我调试的目的,现在一方面查阅书籍,一方面接受大家的建议,希望能把这个程序整好~~

        我的server和client源代码在如下链接中(这是我提的问题,还在解决中,欢迎并感谢点拨一下~~)

http://www.oschina.net/question/437227_51491

继续学习中。。。。

 

转载于:https://my.oschina.net/socket/blog/55389

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值