示例#1
0
文件: udp.c 项目: maikeldoren/js-c
int udp_server_bind(int port, int num, void (*callback)(char * ip, int port, int sock_id, int event, void * buf, int len, void * callbackdata),void *callbackdata)
{
	struct server_arg * arg;
	struct sockaddr_in local;

	arg = (struct server_arg *)malloc(sizeof(struct server_arg));

	udp_startup();

	arg->cb.callback = callback;
	arg->cb.callbackdata= callbackdata;

	local.sin_family=AF_INET;
	local.sin_port=htons(port); ///监听端口
	local.sin_addr.s_addr=INADDR_ANY; ///本机

	arg->socket=socket(AF_INET,SOCK_DGRAM,0);
	bind(arg->socket, (struct sockaddr*)&local, sizeof(local));

	if( create_thread(&arg->thread, server_th, arg) ) {
		closesocket(arg->socket);
		WSACleanup();
		free(arg);
		return -1;
		printf("udp bind error\n");
	}

	return 0;
}
示例#2
0
文件: h316_udp.c 项目: tlhackque/simh
t_stat udp_create (DEVICE *dptr, char *premote, int32 *pln)
{
  //   Create a logical UDP link to the specified remote system.  The "remote"
  // string specifies both the remote host name or IP and a port number.  The
  // port number is both the port we send datagrams to, and also the port we
  // listen on for incoming datagrams.  UDP doesn't have any real concept of a
  // "connection" of course, and this routine simply creates the necessary
  // sockets in this host. We have no way of knowing whether the remote host is
  // listening or even if it exists.
  //
  //   We return SCPE_OK if we're successful and an error code if we aren't. If
  // we are successful, then the ln parameter is assigned the link number,
  // which is a handle used to identify this connection to all future udp_xyz()
  //  calls.
  t_stat ret;
  int32 link = udp_find_free_link();
  if (link < 0) return SCPE_MEM;

  // Make sure WINSOCK is initialized ...
  if ((ret = udp_startup(dptr)) != SCPE_OK) return ret;

  // Parse the remote name and set up the ipaddr and port ...
  if ((ret = udp_parse_remote(link, premote)) != SCPE_OK) return ret;

  // Create the sockets for the transmitter and receiver ...
  if ((ret = udp_create_rx_socket(link)) != SCPE_OK) return ret;
  if ((ret = udp_create_tx_socket(link)) != SCPE_OK) return ret;

  // All done - mark the TCP_LINK data as "used" and return the index.
  udp_links[link].used = TRUE;  *pln = link;
  sim_debug(IMP_DBG_UDP, dptr, "link %d - listening on port %d and sending to %s\n", link, udp_links[link].rxport, udp_format_remote(link));
  return SCPE_OK;
}
示例#3
0
文件: udp.c 项目: maikeldoren/js-c
int udp_send(char * ip, int port, void *buf ,int len) {
	SOCKET           SendingSocket;  
	SOCKADDR_IN      ReceiverAddr;

	udp_startup();

	//创建一个新的套接字来接收数据报   
	if((SendingSocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) == INVALID_SOCKET)  {  
		printf("ERROR:SOCKET failed with %d/n",WSAGetLastError());  
		WSACleanup();  
		return 1;  
	}  
	//建立一个SOCKADDR_IN结构,来识别发送数据报的目的地  
	ReceiverAddr.sin_family = AF_INET;  
	ReceiverAddr.sin_port = htons(port);  
	ReceiverAddr.sin_addr.s_addr = inet_addr(ip);  
	//把一个数据报发送到接收者   
	return  sendto(SendingSocket,(char *)buf,len,0,(SOCKADDR *)&ReceiverAddr,sizeof(ReceiverAddr)) ;
	closesocket(SendingSocket);

}