用C语言写一个接收和发送信息程序的思路,现在不知如何下手,大神给点提示!
用C语言写一个接收和发送信息程序的思路,现在不知如何下手,大神给点提示!
程序代码:#include "stdio.h"
#include "stdlib.h"
#include "Win/winsock2.h"
#include "me/me_net.c"
#define BUF_SEND 2048
void input_pchar (const char* cue, char* arg);
void input_pint (const char* cue, int* arg);
int main (void)
{
WSADATA data;
SOCKET sock;
struct sockaddr_in that;
char that_IP[16];
int that_port;
char buf_send[BUF_SEND] = {0};
WSAStartup_check (0x0202, &data);
sock = socket_check (AF_INET, SOCK_DGRAM, 0);
that.sin_family = AF_INET;
input_pint ("Ta's port: ", &that_port);
that.sin_port = htons ((unsigned short)that_port);
input_pchar ("Ta's IP: ", that_IP);
that.sin_addr.s_addr = inet_addr (that_IP);
while (1)
{
input_pchar ("Send: ", buf_send);
sendto_check (sock, buf_send, BUF_SEND, 0, (struct sockaddr*)&that, sizeof (that));
}
closesocket (sock);
WSACleanup ();
return 0;
}
void input_pchar (const char* cue, char* arg)
{
printf (cue);
fflush (stdin);
fgets (arg, 16, stdin);
}
void input_pint (const char* cue, int* arg)
{
printf (cue);
fflush (stdin);
scanf ("%d", arg);
}
程序代码:#include "stdio.h"
#include "stdlib.h"
#include "Win/winsock2.h"
#include "me/me_net.c"
#define BUF_RECV 2048
void input_pchar (const char* cue, char* arg);
void input_pint (const char* cue, int* arg);
int main (void)
{
WSADATA data;
SOCKET sock;
struct sockaddr_in this;
struct sockaddr_in that;
int that_len = sizeof (that);
char this_IP[16];
int this_port;
char buf_recv[BUF_RECV] = {0};
WSAStartup_check (0x0202, &data);
sock = socket_check (AF_INET, SOCK_DGRAM, 0);
this.sin_family = AF_INET;
input_pint ("Your port: ", &this_port);
this.sin_port = htons ((unsigned short)this_port);
input_pchar ("Your IP: ", this_IP);
this.sin_addr.s_addr = inet_addr (this_IP);
bind_check (sock, (struct sockaddr*)&this, sizeof (this));
while (1)
{
recvfrom_check (sock, buf_recv, BUF_RECV, 0, (struct sockaddr*)&that, &that_len);
printf ("Receive: %s", buf_recv);
}
closesocket (sock);
WSACleanup ();
return 0;
}
void input_pchar (const char* cue, char* arg)
{
printf (cue);
fflush (stdin);
fgets (arg, 16, stdin);
}
void input_pint (const char* cue, int* arg)
{
printf (cue);
fflush (stdin);
scanf ("%d", arg);
}