Programmering 2 TCP
Hej, behöver hjälp med en avdelningsrapport, har letat i boken, på nätet och testat att se om det finns några fel med F10 men allting verkar funka, det som jag undrar över dock är varför programmet skriver ut Bok-[]matris när jag trycker på ladda lista?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Avdelningsrapport
{
public partial class Form1 : Form
{
TcpClient klient = new TcpClient();
int port = 12345;
List<Bok> bokLista = new List<Bok>();
public Form1()
{
InitializeComponent();
klient.NoDelay = true;
if (File.Exists("texter.txt"))
{
StreamReader reader = new StreamReader("texter.txt", Encoding.UTF7, false);
string item = "";
while ((item = reader.ReadLine()) != null)
{
string[] vektor = item.Split(new string[] { "###" }, StringSplitOptions.None);
string titel = vektor[0];
string författare = vektor[1];
string typ = vektor[2];
}
reader.Close();
}
else
{
MessageBox.Show("Filen hittades tyvärr inte.");
}
}
public async void StartaMottag()
{
try
{
IPAddress adress = IPAddress.Parse("127.0.0.1");
await klient.ConnectAsync(adress, port);
buttonServer.Enabled = false;
}
catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
}
public async void StartaLäsare(string message)
{
byte[] utData = Encoding.Unicode.GetBytes(message);
try
{
await klient.GetStream().WriteAsync(utData, 0, utData.Length);
}
catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
}
private void buttonServer_Click(object sender, EventArgs e) // Koppla till server
{
if (!klient.Connected) StartaMottag();
}
private void LaddaLista_Click(object sender, EventArgs e) // Uppdatera listan med böcker från texter.txt filen
{
listBox.Items.Add(bokLista.ToArray());
}
private void listBox_Click(object sender, EventArgs e)
{
if (bokLista.Count > 0)
{
if (listBox.SelectedItems.Count == 1)
{
StartaLäsare(listBox.Text.ToString());
bokLista.RemoveAt(listBox.SelectedIndex);
listBox.Items.RemoveAt(listBox.SelectedIndex);
}
}
}
}
public class Bok
{
public string titel; // Sparar titel
public string författare; // Sparar författare
public string typ; // Spvarar boktyp
public Bok(string titel, string författare) // Vår konstruktor
{
this.titel = titel;
this.författare = författare;
}
} // Bokens underklasser; roman, tidskrit och novellsamling
public class Roman : Bok
{
public Roman(string titel, string författare) : base(titel, författare)
{
typ = "Roman";
}
public override string ToString()
{
return " " + "\"" + titel + "\"" + " " + "av" + " " + författare + "." + " " + "(" + typ + ")";
}
}
public class Tidskrift : Bok
{
public Tidskrift(string titel, string författare) : base(titel, författare)
{
typ = "Tidskrift";
}
public override string ToString()
{
return " " + "\"" + titel + "\"" + " " + "av" + " " + författare + "." + " " + "(" + typ + ")";
}
}
public class Novellsamling : Bok
{
public Novellsamling(string titel, string författare) : base(titel, författare)
{
typ = "Novellsamling";
}
public override string ToString()
{
return " " + "\"" + titel + "\"" + " " + "av" + " " + författare + "." + " " + "(" + typ + ")";
}
}
}
Använder VS i windows 10